Error while trying to upload file to server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

Error while trying to upload file to server

Post by geoffmuskett »

Hi there,
I got this code from About.com, but I'm having trouble implementing it... an issue with my php configuration?? ... this is the error:

Warning: move_uploaded_file(uploads/arrow_right.gif) [function.move-uploaded-file]: failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/testing/upload_image_location_and_data/add.php on line 21
 
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpuzWrQc' to 'uploads/arrow_right.gif' in /Applications/XAMPP/xamppfiles/htdocs/testing/upload_image_location_and_data/add.php on line 21
Sorry, there was a problem uploading your file.


and here's the code:

Code: Select all

<form enctype="multipart/form-data" action="add.php" method="POST">Name: <input type="text" name="name"><br>E-mail: <input type="text" name = "email"><br>Phone: <input type="text" name = "phone"><br>Photo: <input type="file" name="photo"><br><input type="submit" value="Add"></form>

Code: Select all

<?php
 
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['photo']['name']);
 
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
 
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("employees") or die(mysql_error()) ;
 
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;
 
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
 
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
 
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
It seems to me that there is an issue with the folder uploads/ but I'm not sure what! Can anyone pinpoint what is going on?

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error while trying to upload file to server

Post by AbraCadaver »

uploads/ needs to be writable by the web server. If Linux, check chmod or chown.

Also, to eliminate any errors I would use:

Code: Select all

$target = realpath("uploads")."/";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

Re: Error while trying to upload file to server

Post by geoffmuskett »

Thanks for the incredibly quick reply AbraCadaver! I'm using a Mac... is chmod or chown still the right thing to check? (I'm famillar so not sure how to do it but I'll look into it!)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error while trying to upload file to server

Post by AbraCadaver »

geoffmuskett wrote:Thanks for the incredibly quick reply AbraCadaver! I'm using a Mac... is chmod or chown still the right thing to check? (I'm famillar so not sure how to do it but I'll look into it!)
Yes, sorry. OS X is Unix so chmod and chown are there.

chmod will change the permissions of the dir or use chown to change the ownership of the directory to the user that Apache (or your web server) is running as. chmod would most likely be the easiest: http://linuxmanpages.com/man1/chmod.1.php

Code: Select all

chmod 777 /path/to/the/uploads
Or a friendly graphical way: http://docs.info.apple.com/article.html ... mh669.html
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

Re: Error while trying to upload file to server

Post by geoffmuskett »

Brilliant! Thanks for your help AbraCadaver!
Post Reply