PHP upload form : directory is not writable

Maybe to do with the server?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 10964 Views - Last Post: 31 May 2009 - 09:13 AM Rate Topic: -----

#1 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

PHP upload form : directory is not writable

Posted 03 April 2008 - 02:07 PM

Here I thought this should work, until I got an error stating the "/upload/" directory is not writable. Having checked the permissions, it should be hunky dorey.
I've entered persmission to everything I could find.
Anyone have a clue what would cause that?

<?php  
   
 //$_FILES["file"]["type"]  
 $types = array('image/pjpeg','image/tiff','application/pdf','application/msword','application/eps');  
   
 
 $upload_dir = 'www.X.com/upload/';  
   
 if(!is_writable($upload_dir)) exit($upload_dir.' Dir is not writable.');  
   
	 if($_FILES["file"]["size"] > 0)  
	 if ($_FILES["file"]["size"] < 10000000){  
	 if(!in_array($_FILES["file"]["type"],$types)){  
	 echo '"'.$_FILES["file"]["type"].'" File type not allowd.';  
	  
   } else  
   if (is_file($upload_dir . $_FILES["file"]["name"])){  
	   
	 echo '"'.$_FILES["file"]["name"].'" already exists. rename and try again.';  
	   
   } else {	
	   
	   
	 if ($_FILES["file"]["error"] > 0){  
	   echo 'Error: ' . $_FILES["file"]["error"] . '  ';  
		} else {  
	   
	   echo 'Upload: ' . $_FILES["file"]["name"] . '   ';  
	   echo 'Type: ' . $_FILES["file"]["type"] . '  ';  
  echo 'Size: ' . ($_FILES["file"]["size"] / 1024) . ' Kb  ';  
	   
	  echo 'Stored in: ' . $_FILES["file"]["tmp_name"];  
	  if(move_uploaded_file($_FILES["file"]["tmp_name"],$upload_dir . $_FILES["file"]["name"]))  
		 echo '  
 Stored in: ' . $upload_dir . $_FILES["file"]["name"];  
	  else  
		 echo '  
Error moving file to: ' . $upload_dir;  
		  
	   }  
  }  
} else {  
  echo 'File to big.';  
 }  
	  ?>

This post has been edited by rodstol: 03 April 2008 - 03:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP upload form : directory is not writable

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4358
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: PHP upload form : directory is not writable

Posted 03 April 2008 - 02:34 PM

Try throwing in an if statement to see if the directory exists first. This will tell you if there is anything wrong with the file path itself. If this script is on the same machine as the directory itself, you don't need to specify the host name part.

Let us know if the file is coming back as non-existent. Also make sure the capitalization of the directory is proper too. Never know with some of these operating systems.

:)
Was This Post Helpful? 0
  • +
  • -

#3 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 03 April 2008 - 03:47 PM

The directory exists, how can I tell if the file is coming back non-existent? and what does that mean exactly? :crazy:
Was This Post Helpful? 0
  • +
  • -

#4 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: PHP upload form : directory is not writable

Posted 03 April 2008 - 04:06 PM

If I remember correctly, the directory path is relative to the .php file.

So, you need
h]]
$upload_dir = 'upload/'; 



Assuming this code is running in the same folder as the folder "upload".

Per
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4358
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: PHP upload form : directory is not writable

Posted 03 April 2008 - 04:22 PM

View Postrodstol, on 3 Apr, 2008 - 03:47 PM, said:

The directory exists, how can I tell if the file is coming back non-existent? and what does that mean exactly? :crazy:


Is the script telling you exists? I know you can see it on the server and we all know it exists, but the script might not think so. If that path does not exist (php can't see it) then it will make is_writable false.
Was This Post Helpful? 0
  • +
  • -

#6 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 04 April 2008 - 11:34 AM

I changed the path to be relative to the php file and the script completes, however the file isn't uploaded and I don't receive an email notification like I should....
ALSO, when the script is finished, it jacks up the HTML form.

I'd be willing to send the URL to whoever wants to look at it, but I'm not posting the URL. :crazy:
Was This Post Helpful? 0
  • +
  • -

#7 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 08 April 2008 - 02:28 PM

I'm not sure if this is an HTML problem or a PHP problem.

I'm assuming this is the PHP, my upload form runs through the process, but doesn't send the email or upload the file to the predetermined directory.

The second problem may be an HTML issue. When the script completes, it appears a separate page that looks somewhat like the original submit form comes up. But is all jacked up.
Maybe a bad redirect?
Was This Post Helpful? 0
  • +
  • -

#8 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: PHP upload form : directory is not writable

Posted 08 April 2008 - 02:58 PM

Okay, first off, what do you mean by "all jacked up?"

Can you repost your newest PHP along with the HTML form code?

Per
Was This Post Helpful? 0
  • +
  • -

#9 deepamanohar  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-March 08

Re: PHP upload form : directory is not writable

Posted 08 April 2008 - 10:41 PM

hi it seems the problem is with the file uploading path

$upload_dir = 'www.X.com/upload/';

Instead of www.X.com give your document root something which looks like /www/htdocs/home/upload/ . So check your server to find the doucment root and replace it with 'www.X.com'

or

better assign $upload_dir='/upload/' and do the upload

probably either of these two will be solving your problem


To confirm whether the directory exist

use this syntax


if(is_dir(($upload_dir) )
  print "directory exist"
else
 print "directory not exist"




1) check once if your directory is haveing full permission say 777

2) check whether you have given the enctype="multipart/form-data" in the form tag
Was This Post Helpful? 0
  • +
  • -

#10 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 10 April 2008 - 02:41 PM

Quote

Okay, first off, what do you mean by "all jacked up?"

The images aren't showing, the table is about 50px too wide, etc etc. It almost looks like it's loading a separate but similar page, but there aren't any.


if this is above server http root, can't someone delete them?

$upload_dir = 'upload/';


Quote

Can you repost your newest PHP along with the HTML form code?

Certainly!

 <?php  
   
 //$_FILES["file"]["type"]  
 $types = array('image/pjpeg','image/tiff','application/pdf','application/msword','application/eps');  
  

 $upload_dir = 'upload/';  
   
 if(!is_writable($upload_dir)) exit($upload_dir.' Dir is not writable.');  

   
	 if($_FILES["file"]["size"] > 0)  
	 if ($_FILES["file"]["size"] < 10000000){  
	 if(!in_array($_FILES["file"]["type"],$types)){  
	 echo '"'.$_FILES["file"]["type"].'" File type not allowed.';  
	  
   } else  
   if (is_file($upload_dir . $_FILES["file"]["name"])){  
	   
	 echo '"'.$_FILES["file"]["name"].'" already exists. rename and try again.';  
	   
   } else {	
	   
	   
	 if ($_FILES["file"]["error"] > 0){  
	   echo 'Error: ' . $_FILES["file"]["error"] . '  ';  
		} else {  
	   
	   echo 'Upload: ' . $_FILES["file"]["name"] . '   ';  
	   echo 'Type: ' . $_FILES["file"]["type"] . '  ';  
  echo 'Size: ' . ($_FILES["file"]["size"] / 1024) . ' Kb  ';  
	   
	  echo 'Stored in: ' . $_FILES["file"]["tmp_name"];  
	  if(move_uploaded_file($_FILES["file"]["tmp_name"],$upload_dir . $_FILES["file"]["name"]))  
		 echo '  
 Stored in: ' . $upload_dir . $_FILES["file"]["name"];  
	  else  
		 echo '  
Error moving file to: ' . $upload_dir;  
		  
	   }  
  }  
} else {  
  echo 'File to big.';  
 }  
	  ?>
  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>copyprint | submit</title>
<link rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
	background-color: #003300;

}
.head {
	color: #FFCC99;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 18pt;
	vertical-align: text-top;
}
.text {font-size: 12pt;color: #CCCCCC}
.subhead {
	color: #FF9900;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
}
.style8 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; vertical-align: text-top; color: #FFCC99;}
.style9 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9pt; vertical-align: text-top; color: #FFCC99;}
.style13 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: xx-small; vertical-align: text-top; color: #FFCC99; }

-->
</style></head>


<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px" > 
<table width="255" height="650px" border="0" cellspacing="0" cellpadding="20" topmargin="0" leftmargin="0">
  <tr>
	<td width="275" valign="top"><span class="style1">submit </span><br/>
	<span class="style9">send us your documents for printing</span><br/><br/><br/>
	<div>
	  
	  <form id="submit form" name="submit" method="post" enctype="Multipart/Form-Data"  action="http://www.x.com/php/uploadfile.php" >
	  <input type="hidden" name="recipient" value="copydesk@X.com">
	  <input type="hidden" name="subject" value="Document submission">
		<label><span class="style8">name</span><br/>
		<input type="text" hidden="required" name="name" id="name" tabindex="1" />
		  </label><br/>
		  <label><span class="style8">email</span><br/>
		<input type="text"hidden="required" name="email" id="email" tabindex="2" />
		  </label><br/>
			<label><span class="style8">company</span><br/>
		<input type="text" hidden="required"name="company" id="company" tabindex="3" />
		  </label><br/>
		   <label><span class="style8">phone</span><br/>
		<input type="text" hidden="required"name="phone" id="phone" tabindex="4" />
		  </label><br/>
		   <label>
		   <div align="right"><span class="style8">quantity</span>
			 <input name="quantity" type="text" hidden="required" id="quantity" tabindex="5" value="" size="3" maxlength="4" />
		  </div>
		  </label>
		  <label>
	  <div align="right"><span class="style8">size</span> 
				<select name="paper" hidden="required" id="select" tabindex="6">
				   <option>8 1/2 x 11</option>
				   <option>8 1/2 x 14</option>
				   <option>11 x 17</option>
			</select>
		  </div>
		  </label>
		  <label>
	  <div align="right"><span class="style8">paper type</span> 
				<select name="paper2" hidden="required" size="1" id="select" tabindex="7">
				   <option>20lb bond</option>
				   <option>20lb cotton*</option>
				   <option>20lb color*</option>
				   <option>40lb gloss</option>
				   <option>AstroBrite*</option>
				   <option>Skytone*</option>
			</select>
		  </div>
		  </label>
		   <div align="right">
			 <p><span class="style13">* only available in 8 1/2 x 11<br/>We can only accept Microsoft Word, PDF or image files.</span>
			   
			   <input name="document" hidden="required" type="file" size="15" tabindex="8"/>
			  
			   <label>
			   <textarea name="SI" id="SI" cols="27" rows="3" tabindex="9">Specify paper color and other special instructions here.</textarea>
			   </label>
			   <input name="submit" type="submit" value="send" tabindex="10"/>
			   <input type="hidden" name="env_report" value="REMOTE_HOST, REMOTE_ADDR">
			   <input type="hidden" name="redirect"value="/thanks.html" />
			   <br/>
			   <br/>
			 </p>
		  </div>
	  </form>
	  <span class="style8">AstroBrite Colors</span> <br/>
	  <img src="images/Astrobrite/Cosmic Orange.jpg" alt="Cosmic Orange" width="30" /><img src="images/Astrobrite/Fireball Fushia.jpg" alt="Fireball Fuschia" width="30" /><img src="images/Astrobrite/Pulsar Pink.jpg" alt="Pulsar Pink" width="30"/><img src="images/Astrobrite/Planetary Purple.jpg" alt="Planetary Purple" width="30"/><img src="images/Astrobrite/Lunar Blue.jpg" alt="Lunar Blue" width="30"/><img src="images/Astrobrite/Gemini Green.jpg" alt="Gemini Greeen" width="30"/><img src="images/Astrobrite/Terra Green.jpg" alt="Terra Green" width="30"/><br/><br/>
	  
	  <span class="style8">SkyTone Colors</span> <br/>
	  <img src="images/Astrobrite/Bluestone.jpg" alt="BlueStone" width="50" /><img src="images/Astrobrite/Sagebrush.jpg" alt="Sagebrush" width="50" /><img src="images/Astrobrite/Champagne.jpg" alt="Champagne" width="50" /><img src="images/Astrobrite/Natural.jpg" alt="Natural" width="50" /></div>
	  <p align="center" valign="top">		<br/>
		<span class="subhead">Bring your ideas...</span> <br/>
	  <span class="style6">We'll help bring them to life.</span>	  </p>  
  </p>	
  </tr>
</table>
</body>
</html>



if(is_dir(($upload_dir) )
  print "directory exist"
else
print "directory not exist"


I don't think that syntax will work. Thank you though.
Anyway, I already tried the root directory and that didn't work either.
Was This Post Helpful? 0
  • +
  • -

#11 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: PHP upload form : directory is not writable

Posted 10 April 2008 - 03:33 PM

Okay, first off, it's probably just a copy paste error, but you don't have a start <html> or <head> tags...
Your PHP code is above the header too, so it is going to print out your upload message inside or above the header. Put the message into a variable and print the variable part way down the document.

So, I've modified the PHP a bit so it looks nicer. There isn't any reason to have so many nested if statements.

You also weren't checking for submit, so it would have tried to upload every single time. Note that it checks for the post variable "submit" because that was the input tag name of your submit button.
<?php  
	  
	//$_FILES["file"]["type"]  
	$types = array('image/pjpeg','image/tiff','application/pdf','application/msword','application/eps');  
	
	$upload_dir = 'upload/';  
	  
	if(!is_writable($upload_dir)) exit($upload_dir.' Dir is not writable.');  


	if (isset($_POST['submit'])){  
		if($_FILES["file"]["size"] == 0 && $_FILES["file"]["size"] > 10000000){
			echo "File Size Incorrect";
		}
		else if(!in_array($_FILES["file"]["type"],$types)){  
			echo '"'.$_FILES["file"]["type"].'" File type not allowed.';  
		}
		else if (is_file($upload_dir . $_FILES["file"]["name"])){  
			echo '"'.$_FILES["file"]["name"].'" already exists. rename and try again.';  
		}
		else if ($_FILES["file"]["error"] > 0){  
			echo 'Error: ' . $_FILES["file"]["error"] . '  ';  
		}
		else {  
			echo 'Upload: ' . $_FILES["file"]["name"] . '   ';  
			echo 'Type: ' . $_FILES["file"]["type"] . '  ';  
			echo 'Size: ' . ($_FILES["file"]["size"] / 1024) . ' Kb  ';  

			echo 'Stored in: ' . $_FILES["file"]["tmp_name"];  
			if(move_uploaded_file($_FILES["file"]["tmp_name"],$upload_dir . $_FILES["file"]["name"]))
				echo 'Stored in: ' . $upload_dir . $_FILES["file"]["name"];  
			else 
				echo 'Error moving file to: ' . $upload_dir;  
		}  
		  
	}
?>



Now, on to the fixing of the html. In the PHP, when it refers to the info in "$_FILES", it is the same as "$_POST", meaning that the name you give to the input tag is used as the key in the array. All of your PHP looks at "$_FILES['file']", but in your form you have your input tag named "document", so it is never going to see it. So, either change your html, or change the PHP to look in "$_FILES['document']"

You also have a useless <link> tag in your header, above the css. And there is no reason for the form to be an absolute link, you can just do action='' in your form tag, and it will automatically submit back to the same page. It will also head with portability.

Good Luck.

Per
Was This Post Helpful? 0
  • +
  • -

#12 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 10 April 2008 - 04:41 PM

Ok, I've rewritten some of this and took your advice. It seems to have gotten rid of that upload problem and streamlined this code quite a bit, thank you.

However, now I'm getting this ""File type not allowed after uploading a .pdf, .doc and .jpg.

is there something wrong with the type array? can I put 4 or 5 types in one line?

I'm really pretty new to PHP, so I appreciate your help.

This post has been edited by rodstol: 10 April 2008 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: PHP upload form : directory is not writable

Posted 10 April 2008 - 04:59 PM

Since it is printing an empty string between the quotes, $_FILES['file']['type'] must be empty.

Did you change the word 'document' in the html so that it was 'file', or did you update the PHP to be 'document'? Maybe you forgot to update that line with the right name?

A helpful method here to recursively view arrays is "print_r($_FILES)".
It will look ugly in the HTML, but if you look at the source for your page in from a browser, it will be formatted nicely. That will tell you what types are being put in the variable, so you can compare.

Per
Was This Post Helpful? 0
  • +
  • -

#14 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 11 April 2008 - 01:12 PM

Quote

Since it is printing an empty string between the quotes, $_FILES['file']['type'] must be empty.

Well, after handing me a PHP for dummies book, my boss decided i was capable to do this little project. Not to sound like a clueless putz, but which quotes are you referring to?

		  else if(!in_array($_FILES["file"]["type"],$types)){	
			 echo '"'.$_FILES["file"]["type"].'" File type not allowed.';   

Just to clarify, the "file" or "type" should be empty? and doesn't that defeat the purpose?

Quote

Did you change the word 'document' in the html so that it was 'file', or did you update the PHP to be 'document'? Maybe you forgot to update that line with the right name?

<input name="file" hidden="required" type="file" size="15" tabindex="8"/>

I did change it.

This post has been edited by rodstol: 11 April 2008 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#15 rodstol  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 03-April 08

Re: PHP upload form : directory is not writable

Posted 11 April 2008 - 01:19 PM

AHHA!
A missing bracket was the culprit, then changing the "document" to "file" fixed the upload problem.
Thanks a million.

BUT...
I'm still not getting email notification,
	  <input type="hidden" name="recipient" value="mailto:mrx@x.com">  


and the page isn't redirecting properly.
	 <input type="hidden" name="redirect" value="/thanks.html" />   


My PHP for dummies doesn't say anything about HTML forms.
I'll check on W3 schools to see if I can solve this... I may be back.
Thanks so much for the help.

This post has been edited by rodstol: 11 April 2008 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2