Forum Moderators: coopster

Message Too Old, No Replies

Trying to upload graphic

Permissions? Not seeing it in /tmp

         

StupidScript

11:01 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is my code:
<?
print_r($_FILES);
echo "<br />\n";
if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
$thisfile=$_FILES["userfile"]["tmp_name"];
$thisdesc=$_FILES["userfile"]["name"];
$getdb=mysql_connect("localhost","user","password");
$dumpblob=mysql_db_query("test","insert into blobber values (NULL,LOAD_FILE('$thisfile'),'$thisdesc')") or die ("Oops: ".mysql_error()."<br />\n");
}
?>

<form action="<?=$PHP_SELF?>" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" id="userfile" name="userfile" /> <input type="submit" value="Upload Image" />
</form>

This seems to be working, however I do not see the file in the /tmp directory even though it acknowledges "is_uploaded_file".

Trying to dump the temp file uploaded by php into a mysql blob field returns "NULL", so there is (a) no file there, (b) no permission to read the file, or (c) something else.

On a related note, I tried to change the default upload_dir in php.ini to a subdirectory of my web directory, however after restarting the web server (Apache2), the above form still ends up with the uploaded file in the /tmp directory. What am I missing?

Thanks in advance.

dreamcatcher

11:20 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

is_uploaded_file just tests to see if the temp file has successfully been uploaded to the server. You need to use move_uploaded_file to actually move/upload the file to a directory.


if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
$thisfile=$_FILES["userfile"]["tmp_name"];
$thisdesc=$_FILES["userfile"]["name"];
$path = "/home/root/public_html/dir/" . $thisdesc;
move_uploaded_file($_FILES["userfile"]["tmp_name"], $path);

rest of code

Replace the path with your correct path. Think that should work.

StupidScript

7:34 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, dreamcatcher! I also needed to change permissions on the moved file in order to be able to dump it into the database:

if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
$thisfile=$_FILES["userfile"]["tmp_name"];
$thisdesc=$_FILES["userfile"]["name"];
$thispath="/var/www/html/uploads/".$thisdesc;
move_uploaded_file($_FILES["userfile"]["tmp_name"],$thispath);
chmod($thispath,0777);
$getdb=mysql_connect("localhost","userid","password");
$dumpblob=mysql_db_query("test","insert into blobber values (NULL,LOAD_FILE('$thispath'),'$thisdesc')") or die ("Oops: ".mysql_error()."<br />\n");
}

Now I need to get my PHP installation set up to use the GD libraries so I can SEE the graphic data stored in the db as a picture.

I appreciate your help.

dreamcatcher

7:58 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad I could help. Sorry about not mentioning the permissions. :)