Lucas van Dongen
Lucas van Dongen

Reputation: 93

PHP refusing to upload picture

I'm working on a project but when testing the upload pictures function it gives me errors all the way. The site is on a Portal.website.com link (so not www, dunno if that matters)

Anyhow here is the message I get:

Warning: move_uploaded_file(img/uploads/IMG_20160402_244056496.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/xxx/domains/website.com/public_html/portal/artikeltoevoegen.php on line 33

And

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpf277z2' to 'img/uploads/IMG_20160402_244056496.jpg' in /home/xxx/domains/website.com/public_html/portal/artikeltoevoegen.php on line 33

And here is the php part the form itself is pretty standard and works flawlessly

if (isset($_POST['uploadArticle']))
{
    $title = $_POST['title'];
    $article = $_POST['article'];
    $files = $_FILES['files'];
    $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
    $max_file_size = 1024*1000; //1000 kb
    $count = 0;
    foreach ((array) $_FILES['files']['name'] as $f => $name) 
    {     
        if ($_FILES['files']['error'][$f] == 4) 
        {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) 
        {              
            if ($_FILES['files']['size'][$f] > $max_file_size) 
            {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) )
            {
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else
            { 
                // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f],"img/uploads/".$name))
                {
                    $count++; // Number of successfully uploaded file
                    echo $count;
                }
            }
        }
    }
}

Upvotes: 0

Views: 103

Answers (2)

Lucas van Dongen
Lucas van Dongen

Reputation: 93

Okay so it happened to be the server permissions for the folders I was trying to write too. I changed it from 755 to 777 now it works just fine.

Hope someone can use this info

Upvotes: 2

Nicola Pedretti
Nicola Pedretti

Reputation: 5166

As the error suggests the path you are passing to move_uploaded_file does not exist.

If the img folder is in the same directory as the php file you are calling move_uploaded_file from, try using a relative url:

if(move_uploaded_file($_FILES["files"]["tmp_name"][$f],"./img/uploads/".$name))

by adding ./ in front of img

Upvotes: 0

Related Questions