1

I am trying to create a PHP file to upload images to my website. I have the following code, but it is not working. Can anyone help me fix it so I do not get any errors? Is there any better way to do such a thing? I know it is old fashioned.

    $uploaddir = "uploads/images";
    $allowed_ext = "jpg, JPG, png, gif";
    $max_size = "500000";
    $max_height = "4000";
    $max_width = "4000";

    $extension = pathinfo ($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $allowed_ext);
    for ($i = 0; $i <= count($allowed_paths); $i++){
            if ($allowed_paths[$i] == "$extension"){
            $ok = "1";
        }
    }
    if ($ok == "1"){
        if($_FILES['file']['size'] > $max_size){
                print "Your file is too big!";
                exit;
            }
        }

    if($max_width && $max_height){
        list($width, $height, $type, $w) = getimagesize($_FILES['file']['name']);
        if ($width > $max_width || $height > $max_height){
                print "Your file width/height are too big!";
                exit;
            }

        }
    if (is_uploaded_file($_FILES['file']['tmp_name'])){
        move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.'/'.$_FILES['file']['name']);

        print "Your file was uploaded successfully :)";
        }
                else
                    print "Wrong extensin";



    ?>

When I run the script I get the following error:

Warning: getimagesize(1 (8).jpg) [function.getimagesize]: failed to open stream: No such file or directory in D:\Hosting\8923686\html\uploadedimages\upload.php on line 25

Warning: move_uploaded_file(uploads/images/1 (8).jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\phpE5F5.tmp' to 'uploads/images/1 (8).jpg' in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33

or

invalid file

Can any one please tell me where is my problem?

2
  • 1
    Get proper permission to the files and folders you are writing to. (Line 33, where you are doing move_uploaded_file), you don't have permission to access the destination. Maybe it doesn't exist. Commented Mar 31, 2012 at 19:56
  • is any other thing than the 777 permission ? Commented Mar 31, 2012 at 21:37

2 Answers 2

2

As for the first error, change getimagesize($_FILES['file']['name']) to getimagesize($_FILES['file']['tmp_name']).

As for the other two errors, make sure your script has permissions to write files to the folder where you're trying to move the uploaded file to.

Edit: Also try to use a full absolute path in $uploaddir instead of just uploads/images. Since you're using a relative path from a script that isn't located in the document root, it's possible that PHP is looking in the wrong directory.

Sign up to request clarification or add additional context in comments.

2 Comments

I did now I am getting this:Warning: move_uploaded_file(gagsallday.com/uploadedimages/uploads/images//D:\Temp\php\php6AF0.tmp) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php6AF0.tmp' to 'gagsallday.com/uploadedimages/uploads/images//D:\Temp\php\php6AF0.tmp' in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33
It has to be an absolute path on the same system, not a URL. You can't move your uploaded file to a remote website. If the path differs between your development system and your live website, add some code to detect where your PHP file is running and adjust $uploaddir accordingly.
1

getimagesize($_FILES['file']['name'] in line 25 is the first culprit: you meant getimagesize($_FILES['file']['tmp_name']

For the othe errors you need to check permissions of uploads/images - they are obviously off limits for the webserver user.

4 Comments

in filezilla I gave it the 777 permission. What also should I do ?
777 is a UNIX-style permission, which might not be applicable to the underlying Windows file system. I recommend to delete the uploads/images, and maybe even the uploads directory and to creat it from PHP in a one-off script - this way you can be sure, the webserver has the necessary privileges.
I am not sure what you mean by one-off script ? how do I do such thing ?
I meant a script, that is run only once (to create the directories)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.