0

I have a file upload script:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["picture"]["name"]));
if ((($_FILES["picture"]["type"] == "image/gif") || ($_FILES["picture"]["type"] == "image/jpeg") || ($_FILES["picture"]["type"] == "image/jpg") || ($_FILES["picture"]["type"] == "image/png")) && in_array($extension, $allowedExts)):
    if($_FILES["picture"]["error"] > 0):
        echo "Error: " . $_Files["picture"]["error"];
    else:
        move_uploaded_file($_FILES["picture"]["tmp_name"], "/TnA/ProfilePics/" . $_SESSION['ID'] . "." . $extension);
    endif;
endif;

But I am getting the errors:

Warning: move_uploaded_file(TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5nOdhI' to 'TnA/ProfilePics/1.jpg' in /home/content/91/9848191/html/TnA/webservice.php on line 1067

Here is my file structure:

Web Host Root (I have another site here)
    -TnA (Root of this site)
        -index.html
        -webservice.php
        -ProfilePics
            -(My target location)

What relative directory url should I be using? I have tried ProfilePics/1.jpg and /ProfilePics/1.jpg both result in the same error.

EDIT:

Using:

move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__) . "ProfilePics/" . $_SESSION['ID'] . "." . $extension);

I get:

Warning: move_uploaded_file(/home/content/91/9848191/html/TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/91/9848191/html/TnA/webservice.php on line 1067

1 Answer 1

3

Most likely that you have problem using relevant paths. If your upload script is webservice.php then you are actually trying to put file into / %webHostRoot% / TnA / TnA / ProfilePics / directory. Use full path in move_uploaded_file()'s target. Try to use something like:

move_uploaded_file($_FILES["picture"]["tmp_name"], dirname(__FILE__)."ProfilePics/" . $_SESSION['ID'] . "." . $extension);

UPD: Here's func to create dirs recursively:

function MkDirTree($path,$permissions = 0755, $compat_mode=true) {
    if (!$compat_mode) {
        $dirs = split("/",$path);
        $path = "";
        foreach ($dirs as $key=>$dir) {
            $path .= $dir."/";
            if ($dir!="" && !is_dir($path)) exec("mkdir -m ".$permissions." ".$path);
        }
    } else {
        $dirs = split("/",$path);
        $path = "";
        foreach ($dirs as $key=>$dir) {
            $path .= $dir."/";
            if ($dir!="" && !file_exists($path)) mkdir($path, $permissions);
        }
    }
    return file_exists($path);
}
Sign up to request clarification or add additional context in comments.

4 Comments

If I use the full http:// address I get Warning: move_uploaded_file(http://****.net/TnA/ProfilePics/1.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in /home/content/91/9848191/html/TnA/webservice.php on line 1067
You should use server's internal absolute path, not your website's URL. I've edited my answer with the code example.
Ok, I got it... Somehow my FTP did not create the directory ProfilePics. So obviously... it didn't exist. It's fixed now, but your answer did help! Thanks.
Welcome! I added a little function to my answer, allowing to create dirs recursively. Hope it'll be useful for you. Good luck!

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.