File Upload question, file doesn't show up on success

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
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

File Upload question, file doesn't show up on success

Post by jdhorton77 »

I know that subject line doesn't logically make since, but here's my situation. I have a file upload function that supposed to upload an image to a folder on the server called uploads. When I hit the submit button the process goes through like it should. But when I look in the folder, the image is not there. When I use the move_uploaded_file function, am I supposed to use an absolute address to the folder with http://somewhere.com/uploads/ ?

My http folder has the upload file along with the uploads folder. So I don't think I have to step out or anything. I appreciate all the help you guys give us newbs.

Code: Select all

if ($_FILES["file"]["error"] > 0)
{
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	
}
     else
{
     $sql = "INSERT INTO images (imagenum, usernumber,imagename,category,public)
	values (null,'$usernumber','$imagename','$category','$public')";
					
	if (!mysql_query($sql,$connection)) {
	    die('Error: ' . mysql_error());
	}
	else {
	    $success = "Upload Successfull";

	    move_uploaded_file($_FILES["file"]["tmp_name"],
	    "uploads/" . mysql_insert_id());                                 // <---- question here 
	}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP is not able to natively write files over HTTP requests without added help. Local paths are all you should require.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

So then it should be putting the image in the folder specified then right? If for some reason it couldn't see the folder, wouldn't it throw an error. Thanks for the help feyd
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If it is unable to write to the location (for many reasons) it will throw an error.
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Well I know the folder is there. So I put it in an if statement and the function is returning false. Can you look at my statement and tell if I have it right?

Code: Select all

$iname = mysql_insert_id();
echo $iname;                        // I have this here just to make sure it wasn't null
				
if(!move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $iname)) { echo "failed"; }
       else { echo "success"; }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure PHP has the proper permissions to write files into that folder?
User avatar
jdhorton77
Forum Commoner
Posts: 56
Joined: Tue Nov 07, 2006 3:29 pm
Location: Charlotte, NC

Post by jdhorton77 »

Ahhh yeah, well, I was just about to say thanks for the help. And you can call me stupid now. I guess before you upload a file you should make sure it has permission to do so. Sorry guys. But one more question, it saved it as a binary file. Is that right even though it was a jpeg? Just wondering if I'll still be able to display on a page as a jpeg. Thanks again.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

is it saved as a blob?
Then you can use get file content,
make sure to set the header right for that filetype.
Post Reply