Jump to content

Problem with uploading mutiple files


roxki

Recommended Posts

Hello dear PHP users.

I'm currently making an Gallery for a friend, or atleast I was, until I ran into a pretty annoying problem.

He wanted to be able to upload multiple files at once, so I made an HTML form like this (just an example):

 

<form action="" enctype="multipart/form-data" method="post">
File1: <input type="file" name="picture[]"><br>
File2: <input type="file" name="picture[]"><br>
File3: <input type="file" name="picture[]"><br>
<input type="submit" value="Upload">
</form>

 

At the PHP I'm running the array throuh a while where I do some file-type checks and file-size checks before starting to upload, which I've no trouble at all with.

But then my script returns my error message because my move_uploaded_file() function won't move the uploaded file.

So first I check $_FILES["picture"]["error"] for any errors, but it returns a '0'.

Second I check the CHMOD on my specified destination directory which got 7,7,7 permission. So shouldn't be a problem either.

Third I do a file_exists() on the destination directory which returns '1', so there's nothing wrong with that directory either.

 

So at the moment I'm pretty lost, I've made many other uploading systems, well which only upload one file at once.

The script doesn't come with any warnings, just the message I told it to say IF move_uploaded_file() went wrong.

 

Have I forgot something important? I've been sitting with this for several hours now without any luck at all.

Hope you can help me out, 'cause this is freaking me out!

Link to comment
Share on other sites

Some codes for those who should be interested in that:

 

$upload_dir		= "images/galleri/".$data_galleri->navn;
$thumb_dir		= "images/galleri/thumb/".$data_galleri->navn;

foreach($_FILES["pic"]["error"] as $i => $fil)
{
  if(check_file("pic", $_FILES["pic"]["name"][$i], $_FILES["pic"]["type"][$i], $_FILES["pic"]["size"][$i]) == true)
    {
      $sql_do = "insert into galleri_pic(navn) values('$data->navn')";
      if(mysql_query($sql_do))
      {
        $used_id	= mysql_insert_id();
        $tmp_file	= $_FILES["pic"]["tmp_name"][$i];
        $upload_img	= $upload_dir."/".$used_id.".jpg";

        if(move_uploaded_file($tmp_file, $upload_img))
        {
          if(make_thumb($upload_img, $data_settings->thumb_h, $data_settings->thumb_w, $thumb_dir, $used_it, $data_settings->quality))
          {
            echo "Picture has been uploaded";
          }
          else
          {
            if(mysql_query("delete from galleri_pic where id = '$used_id'"))
            {
              echo "Couldn't upload the picture<br>";
            }
          }
        }
        else
        {
          if(mysql_query("delete from galleri_pic where id = '$used_id'"))
          {
            echo "An error occured during uploading of the file<br>";
          }
        }
      }
    }
    else
    {
      echo "The picture doesn't fit the requirements.<br>";
    }
}

Link to comment
Share on other sites

Okay forgot that in the code I've 2 selfmade functions, don't look at them, they work fine..

And just to get back to the topic, as far as I can see it's the move_uploaded_file() part which doesn't work as it should.

 

if(move_uploaded_file($tmp_file, $upload_img))
{
  if(make_thumb())
  {
  echo "Picture has been uploaded";
  }
}
else
{
  echo "This is the error message I keep getting!";
}

Link to comment
Share on other sites

I think you need to put $i++ somewhere in there, otherwise it's going to keep trying to upload $_FILES["pic"]["tmp_name"][0];

 

I've tried make the script output the 2 variables used in the move_uploaded_file() function, $tmp_file and $upload_img.

They both show the data from its own array. Not the same data as it would've been if it worked as you mentioned.

But I'll try it out anyways. At the moment I'm open for any help!

Link to comment
Share on other sites

Your $i's are fine. If move_uploaded_file is returning false, and no error is being outputted, then you don't have error reporting turned on. Turn that on and let us know what errors you get. Also, try adding this code right before the move_uploaded_file and see what happens:

 

if(!is_dir($upload_dir)) die("Upload dir doesn't exist: {$upload_dir}");
if(!is_writable($upload_dir)) die("Upload dir is not writable: {$upload_dir}");
echo "TMP FILE: {$tmp_file}\n";
echo "NEW FILE: {$upload_img}\n";

 

Link to comment
Share on other sites

Could be something about error reporting aren't turned on. And I'm not sure if I've permission to it, since I'm not hosting this site myself..

And about the dir and access.. well, as I wrote above then I've done file_exists($upload_dir) AND checked that I've full permission to the directories (CHMOD 777).

Could you please tell me the exact command to turn on the error reporting?

Link to comment
Share on other sites

Okay just did a check on is_dir($upload_dir) and one on is_writeable($upload_dir) which both returned '1' (true).

On this try the:

- TMP FILE: /tmp/phpbSwaEJ ($tmp_file)

- NEW FILE: images/galleri/test/115.jpg ($upload_img)

Nothing surprised here..

But the error reporting did surprise me, alot!:

Notice: Undefined variable: data in /hsphere/local/home/roxki/test.krelle.dk/v6/admin/galleri.php on line 288 Warning: move_uploaded_file(): SAFE MODE Restriction in effect. The script whose uid/gid is 176418/176418 is not allowed to access /hsphere/local/home/roxki/test.krelle.dk/v6/images/galleri/test owned by uid/gid 398/398 in /hsphere/local/home/roxki/test.krelle.dk/v6/admin/galleri.php on line 295 

 

So it says that it's not allowed access to the directory, how can that be? I've given every single folder, from the root -> the specified directory full permission. AND I've upload systems like this on this webserver which work without any problem at all...

Link to comment
Share on other sites

Okay that confused me alot.. So I read your link a couple of times and still don't understand much of that..

But I went to my webhoster's support site where I could see that I won't be able to turn Safe mode Off, if it was that you were thinking about.

Second I also saw that there shouldn't be any problems at all with file uploading aslong I just write the directory-path in the right way (relative).

So I was thinking maybe you've an solution how to be the right owner to avoid this error?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.