Hi,

I've been busy writing a script to upload photo's to my webserver, and after a lot of searching I finally managed to get the permissions correct, but now I'm having another problem. There are quite a lot of posts about it, but they all come down to having the wrong path, but for as far as I can see the server is crazy :P anywho, errors (last is output of my script):

Warning: move_uploaded_file(contacts/banner.png) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/xxx/domains/xxx.nl/public_html/test/contacts/maakprof.php on line 23

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpZ20V6B' to 'contacts/banner.png' in /home/xxx/domains/xxx.nl/public_html/test/contacts/maakprof.php on line 23
Copy unsuccessful

and my php script:

$con = mysql_connect("localhost","name","pass");
if(!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("dbname", $con);

//define a max size for the uploaded images in Kb
define ("MAX_SIZE","75"); 
if(isset($_FILES['foto']['name']) && $_FILES['foto']['name']<>""){
	$typ = $_FILES['foto']['type'];
	if($typ == "image/gif" || $typ == "image/png" || $typ == "image/jpeg" || $typ == "image/pgif" || $typ == "image/ppng" || $typ =="image/pjpeg")
	{
		$uploaddir = "contacts/";
		$uploadimages = $uploaddir.basename($_FILES['foto']['name']);
		if(move_uploaded_file($_FILES['foto']['tmp_name'], $uploadimages)){
			echo "File successfully copied";
			$sql="INSERT INTO contacts (Naam, Leeftijd, Foto, Beschrijving)
			VALUES ('$_POST[naam]','$_POST[leeftijd]','$uploadimages',
                                '$_POST[beschrijving]')";
			if (!mysql_query($sql,$con)){
				die('Error: ' . mysql_error());
				mysql_close($con);
			}
		}
		else{echo "Copy unsuccessful";}
	}
	else{ 
		echo "Incorrect file type";
	}
}
else{
	echo "No file selected";
}
?>

and my html form:

<form name="loginform" action="contacts/maakprof.php" enctype="multipart/form-data" method="post"> 
 
 <div align="center">
   <table width="400" border="0">
     <tr>
       <td><table width="200" border="1" align="center">
         <tr>
           <td>Naam
             <input type="text" name="naam" maxlength="50"  /></td>
           <td>Leeftijd
             <input type="text" name="leeftijd" maxlength="3" /></td>
         </tr>
       </table></td>
     </tr>
     <tr>
       <td><div align="center">Foto
         <input name="foto" type="file" size="35" />
       </div></td>
     </tr>
   </table>
   
 </div>
 <p align="center">Beschrijving<br />
 <textarea name="beschrijving" cols="75" rows="15" ></textarea>
 </p>

 <div align="center">
   <input type="submit" value="Voeg toe" />
 </div>
</form>

The reason I've put the php file to process the data in the folder where the pictures must go is that I only have to extend permission for that folder. I'm kind of new with the direct access to the server using php, so any advice is welcome.
Note: Everything works except uploading the file to the server.

Thanks in advance,

Regards,

Anti

Recommended Answers

All 6 Replies

Very strange, now, I havent changed anything all of a sudden I have a permission denied error again.. hmm :

Warning: move_uploaded_file(contacts/header.png) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/xxx/domains/xxx.nl/public_html/test/contacts/maakprof.php on line 21

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpS8pNho' to 'contacts/header.png' in /home/xxx/domains/xxx.nl/public_html/test/contacts/maakprof.php on line 21
Copy unsuccessful

now I dont get it anymore :S

fixed, my own fault, since I copied the maakprof to the folder, I couldve removed

$uploaddir = "contacts/";

,

so it was looking for a folder contacts in contacts

hmm when trying out a bit i'm still a bit confused, seemed it had something to do with the permissions too, because the only way I can get it to work is using 777 permissions on the folder and 775 on the maakprof.php. I dont know if this is normal?

Thanks for the time in advance for answering this probably noob question :)

Update:

finally got the uploading to work and after a bit of searching I noticed that to also be able to delete files I need to use FTP :P (kind of new in this kind of webdesign), so after some looking around I ran into a ftp upload script, but I cant seem to get it to work. I think I know where the error is but I dont know why this is an error :) The thing is that it doesnt go into the first if because of the isset($_POST["submit"]).

code:
html form (kind of the same)

<form name="newprofform" action="contacts/maakprof.php" enctype="multipart/form-data" method="post"> 
 
 <div align="center">
   <table width="400" border="0">
     <tr>
       <td><table width="200" border="1" align="center">
         <tr>
           <td>Naam
             <input type="text" name="naam" maxlength="50"  /></td>
           <td>Leeftijd
             <input type="text" name="leeftijd" maxlength="3" /></td>
         </tr>
       </table></td>
     </tr>
     <tr>
       <td><div align="center">Foto
         <input name="foto" type="file" size="35" />
       </div></td>
     </tr>
   </table>
   
 </div>
 <p align="center">Beschrijving<br />
 <textarea name="beschrijving" cols="75" rows="15" ></textarea>
 </p>

 <div align="center">
   <input name="submit" type="submit" value="Voeg toe" />
 </div>
</form>

php:

ini_set("upload_max_filesize","100M");
ini_set("file_uploads","1");
ini_set("upload_tmp_dir","/tmp");
ini_set("post_max_size","180M");

//if(isset($_POST["submit"])!="submit"){}
//echo $_FILES['foto']['name'];
if(isset($_POST["submit"])){echo "tochwel";}
echo isset($_POST["submit"]);
if((isset($_POST["submit"]))&&($_FILES['foto']!="")){
	$ftp_user_name="xxx"; //change to ftp username
	$ftp_user_pass="xxx"; //change to ftp password
	$ftp_server="xxx"; //change to ftp url
	$ftp_dump_dir="/contacts"; //change to destination directory
	$db_name="xxx"; // Database name
	$sql_save_dir="contacts";

	
	$con=mysql_connect("localhost", "$ftp_user_name", "$ftp_user_pass")or die("cannot connect");
	mysql_select_db("$db_name")or die("cannot select DB");
		if($_FILES['foto']['name']!="none"){
			if($_FILES['foto']['size']!=0){
				if(is_uploaded_file($_FILES['foto']['tmp_name'])){
					echo "file ".$_FILES['foto']['name']." uploaded!<br>";
					$conn_id = ftp_connect($ftp_server);
	
					$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
					if ((!$conn_id) || (!$login_result)) {
						echo "FTP connection has failed!<br>";
						echo "Attempted to connect to $ftp_server for user $ftp_user_name";
						exit;
					} else {
						echo "Connected to $ftp_server! <br>";
						//set PASV mode
						if(!ftp_pasv($conn_id,TRUE)){
							echo "Could not enter PASV mode!";
						}
						//rename to file#_date.Ext
						$filename = $_FILES['foto']['name'];
						
						$path=$sql_save_dir.'/'.$filename;
						
						//$filename.= ".".Get_extension($_FILES['foto']['name'][$x],3);
						//change directory
						if (@ftp_chdir($conn_id, $ftp_dump_dir)) {
						//maybe you want to make sure we are in the correct directory
						echo "Current directory is now : ", ftp_pwd($conn_id), "\and";
					} else {
						//you want to know if it didn't work
						echo "Couldn't change directory\and";
					}
					//upload the file and let the user know what happened
					if(ftp_put($conn_id,$filename,$_FILES['foto']['tmp_name'],FTP_BINARY)){
						$sql="INSERT INTO contacts (Naam, Leeftijd, Foto, Beschrijving)
						VALUES
						('$_POST[naam]','$_POST[leeftijd]','$path','$_POST[beschrijving]')";
						if (!mysql_query($sql,$con)){
							die('Error: ' . mysql_error());
							
						}
						mysql_close($con);
						
					}else{
						echo "There was a problem sending file ".$_FILES['foto']['name']."<br>";;
					}
				}
				// close the FTP stream
				ftp_close($conn_id);
			}
			else echo"File was not uploaded!<br>";
		}
		echo "<br>";
	}

}


?>

Note:The sql works, just the uploading of the file doesnt.
Any help is welcome,
Anti

Im having the same issue as you. Thing is I dont have a clue on how to set permisions. Where do I start?

When you upload something through PHP, then on the server the upload is done by the process which is running the webserver. This process usually is associated with a user called www or apache or www-data etc. This user has to have write permissions to the directories involved, otherwise your upload will fail.
So you can either assign the upload directories to this user and make them user-writeable (755), or you can assign them to the same group as the one who created the site and make them group-writeable (775), or you make them world-writeable (777).
To set permissions you have (in linux) to log in as root to your server and use the chmod command.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.