开发者

Having probles uploading a picture to a folder

开发者 https://www.devze.com 2023-03-05 05:55 出处:网络
I initialize the variables, use move_uploaded_file function and then I write a basic form user will use to upload their photo. I am trying to print at lease an error_msg or a sucess_msg showing wether

I initialize the variables, use move_uploaded_file function and then I write a basic form user will use to upload their photo. I am trying to print at lease an error_msg or a sucess_msg showing wether they had sucess in loading the file or not. nothings is showwing up. Help!

           $error_msg = "";
           $success_msg = "";
           $name2 = "";


       if ($_POST['parse_var'] == "pic"){

        if (!$_FILES['fileField']['tmp_name']) { 

            $error_msg = '<font color="#FF0000">ERROR: Please browse for an image before you press submit.</font>';

        } else {

            $maxfilesize = 51200; // 51200 bytes equals 50kb
            if($_FILES['fileField']['size'] > $maxfilesize ) { 

                        $error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {

                        $error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else { 

                        $newname = $id'.jpg';
                        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "images/$id/".$newname);
                        $success_msg = '<font color="#009900">Your image has been updated, it may take a few minutes for the changes to show... please                                                    be patient.</font>';
            }

        } // close else that checks file exists

}


    <table width="709" align="center" cellpadding="5">
            <form action="edit_profile.php" enctype="multipart/form-data" method="post" name="pic1_form" id="pic1_form">
          <!--    <tr>
                <td width="125" class="style7"><div align="center"><strong>Please Do First &rarr;</strong></div></td>

              </tr>-->
              <tr>
            <td width="16%"><?php print "$user_pic"; ?></td>
            <td width="74%">
              <input name="fileField" type="file" class="formFields" id="fileField" size="42" />
              50 kb max
           </td&g开发者_如何学编程t;
            <td width="10%">
              <input name="parse_var" type="hidden" value="pic" />
              <input  type="submit" name="button" id="button" value="Submit" />
            </td>
           </tr>
           </form></table>


You're going about checking if an upload was performed the wrong way:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... we're in a post situation, so check if a file was uploaded ...
   if ($_FILES['fileField']['error'] !== UPLOAD_ERR_OK) {
      $error_msg = "File upload failed with error code " . $_FILES['fileField']['error'];
   } else {
      ... a file was successfully uploaded ...
      ... process it ...
      if (!move_uploaded_file( ... )) {
        $error_msg = "Failed to move file";
      }
   }
}

Checking for the presence of a form field is a poor way to check if a POST was performed. you may forget to put that field into the form, the name could change, etc... Checking the REQUEST_METHOD is guaranteed to work, as that is set regardless of what kind of request is being performed, and will always be the type of the request (get, post, head, etc...).

As well, don't use the user-provided file name to verify that they uploaded an image. That name can be trivially forged by simply renaming some OTHER kind of file to "whatever.jpg". Use server-side means to check if it's an image, such as FileInfo or getimagesize()


I think the error you're looking for is this one:

$newname = $id'.jpg';

You forgot a dot, and the whole script just crash:

$newname = $id.'.jpg';


I don't see anywhere in that code where you are echoing the error or success message.

0

精彩评论

暂无评论...
验证码 换一张
取 消