PDA

View Full Version : Uploading images with PHP


cocoonfx
03-25-2007, 04:51 AM
Hello


I have used this script before to test about a year ago. I now actually have a need for the script now and for some reason its comes up with an error. I have checked the permissions on my web server there correct (0777).

HTML Part:


<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30">
<input type="submit" value="Upload!">
</form>
</body>
</html>


PHP Part:


<?php
$file_dir="/home/toxics/public_html ";
foreach($_FILES as $file_name=> $file_array) {
echo "path: ".$file_array["tmp_name"]."<br>\n";
echo "name: ".$file_array["name"]."<br>\n";
echo "type: ".$file_array["type"]."<br>\n";
echo "size: ".$file_array["size"]."<br>\n";
If(is_uploaded_file($file_array["tmp_name"])) {
move_uploaded_file($file_array["tmp_name"],
"$file_dir/$file_array[name]") or die ("couldn't copy");
echo "file was moved!<br><br>";
}
}
?>


The error message:
Warning: move_uploaded_file(/home/toxics/public_html /26sept2006c.JPG) [function.move-uploaded-file (http://www.toxictshirt.co.uk/function.move-uploaded-file)]: failed to open stream: No such file or directory in /home/toxics/public_html/upload.php on line 11



Warning: move_uploaded_file() [function.move-uploaded-file (http://www.toxictshirt.co.uk/function.move-uploaded-file)]: Unable to move '/tmp/phpQzeKRg' to '/home/toxics/public_html /26sept2006c.JPG' in /home/toxics/public_html/upload.php on line 11
couldn't copy

The permission are set correctly but i don't why its coming up with this message. Could any suggest why this is happening?

davidj
03-25-2007, 09:47 AM
first you need this in your form...


<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

davidj
03-25-2007, 09:49 AM
and does this require a end slash like this ...

$file_dir="/home/toxics/public_html/ ";



if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

$newname = "../tmp/".$_SESSION['MM_Username']."filename.".$extension;

copy($_FILES['userfile']['tmp_name'], $newname);

chmod($newname,0777);

}


<input name="userfile" type="file" class="form-field-upload">

note the name of the file field (userfile) is called in the is_uploaded_file function and the copy function

also note the chmod straight after

cocoonfx
03-25-2007, 05:25 PM
Thanks that seems to work.


How would i change the "filename." to the actual name of he file as when i look on the webserve i have filename. and it over rights this everytime?


Regards
James