Jump to content

Recommended Posts

Total Nood to PHP trying to write a multiple image upload script, however having problem as the image validation kills the script and it wont upload any ideas?

 

$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); //These will be the types of file that will pass the validation.
$upload_path = '../pic_upload/'; // The place the files will be uploaded to.

foreach ($_FILES["pictures"]["error"] as $key => $error)

{

$filename = $_FILES['pictures']['name'][$key];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if ($_FILES["pictures"]["size"][$key] >= 700000) {      //Check the Size
   die ('size to big');
}

if (empty($filename)) {                                                       //Is the field empty
    die ('one of the fields is empty but the rest have been upload');
}

if(!in_array($ext,$allowed_filetypes))                                           //Is the file Allowed
      die('The file you attempted to upload is not allowed.');

if ($error == UPLOAD_ERR_OK) {                                                  //Upload File check for errors
       echo"$error_codes[$error]";
       move_uploaded_file($_FILES["pictures"]["tmp_name"][$key],$upload_path . $_FILES["pictures"]["name"][$key]) or die("Problems with upload");
   }

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/
Share on other sites

The following is the basis for the code you are using (gotten from the php.net documentation.) You would put your application level validation logic inside the if(){...} statement, because you can only access and check the uploaded file information when the file was actually uploaded without any errors -

 

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "data/$name");
    }
}
?>

 

The code you have now makes no sense.

The following is the basis for the code you are using (gotten from the php.net documentation.) You would put your application level validation logic inside the if(){...} statement, because you can only access and check the uploaded file information when the file was actually uploaded without any errors -

 

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "data/$name");
    }
}
?>

 

The code you have now makes no sense.

 

Totally Confused now :-s

 

The code works and uploads the files ok however, the script is for multiple file upload and if the user uploads a file to form field 3 for example and form field 1 is left empty the script dies and no files are uploaded i belive it is this part that is killing it

   if (empty($filename)) {                                                       //Is the field empty
    die ('one of the fields is empty but the rest have been upload');
}

 

any ideas how to get round this?

the script dies

 

Of course, because you are using the die() statement to display validation errors and your code will die on the first error that is found.

 

To validate multiple pieces of user data, you should use an array to both hold validation error messages and to serve as a flag that errors have occurred.

 

<?php
$errors = array(); // define an array to hold errors

if(some_validation_condition_is_not_met){
    $errors[] = "user message you want to display for this error";
}

// check if there were any validation errors -
if(empty($errors)){
  // no errors, use the data here...

}

 

You can loop over the $errors array to display any errors that have occurred.

 

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.