File upload problem : need help to identify an error
| From: | Philarmon | Date: | Tue, 20 May 2003 22:35:17 +0000 |
| Subject: | File upload problem : need help to identify an error | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148335@lists.php.net to get a copy of this message | ||
Hello !
First, im pretty weak at PHP so go easy with me :)
I have a strange problem with uploading an image to my server using PHP and
cant figure out why - maybe somebody could help me here
I have a basic script that checks some image parameters (size, extension
etc) and if everything is ok, uploads it. Everything works fine when i test
it as it is but when im trying to implement it in my
Link-directory-templates i get an error. Actually, i would be glad if i knew
at least whats wrong.
I have an input form with an input field called "userfile" and then the
variables are passed to the PHP script.
Here is what im getting when i try it out as it is and everything works:
file: /tmp/phpnwzpMe
filename: screen7.jpg
extension: jpg
width: 170
height: 107
screen71053468303.jpg copied succesfully !!
And here is the output with the error:
file: D:\\Flash\\flashvista\\cdrom.gif
filename:
extension:
Warning: getimagesize(D:\\Images\\screen7.jpg) [function.getimagesize]:
failed to create stream: No such file or directory in
/is/htdocs/24590/www.MYDOMAIN.com/index.php(15) : eval()'d code on line 36
width:
height:
Wrong file extension ! Allowed are .gif, .png and .jpg
File NOT copied
The major questions (why i think it doesnt work):
1. Why in the first case im getting the temporary filename on my server and
in the second one - the filename on my computer (with some strange "\\")
2. Could it be that the script does not find the file on my computer ?
3. Could it be that the script does not find the directory on the server and
cant get the image parameters from there ?
And here is my script
echo "file ".$userfile."<br>";
echo "filename ".$userfile_name."<br>";
$error_message = "";
$maximagewidth = 170;
$maximageheight = 107;
// check file size
if ($userfile_size > 6144){
$error_message ="<br>This image is too big! The maximum file size is 6144
Bytes. Your file has ".$userfile_size;
}
// check filename extension
$ext=explode('.',$userfile_name);
$ext=strtolower($ext[count($ext)-1]);
if ($ext != "gif" && $ext != "png" && $ext != "jpg"){
$error_message .= "<br>Wrong file extension ! Allowed are .gif, .png and
.jpg";
}
echo "extension:".$ext."<br>";
// check width/height
$imagehw = GetImageSize($userfile);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
if ($imagewidth > $maximagewidth || $imageheight > $maximageheight){
$error_message .= "<br>Maximal image size (width x height) is
".$maximagewidth." x ".$maximageheight." pixels. Your image has
".$imagewidth." x ".$imageheight." pixels.";
}
echo "width: ".$imagewidth."<br>";
echo "height: ".$imageheight."<br>";
// copy the file to the server
if ($error_message == ""){
// remove spaces
$userfile_name=str_replace(chr(32),chr(95),$userfile_name);
// add timestamp to the filename
$temp = explode('.',$userfile_name);
$userfile_name = $temp[0].time().".".$temp[1];
// copy to this directory
$dir="screens/";
copy($userfile,$dir.$userfile_name);
if (!is_uploaded_file ($userfile)){
echo "<b>$userfile_name</b> couldn't be copied !!";
} else{
echo "<b>$userfile_name</b> copied succesfully !!";
}
}
//display error message
if ($error_message != ""){
echo $error_message."<br><br>File NOT copied";
}
Thank you so much for your time !
Peace,
Philarmon