0

(beginner)

I'm having trouble uploading my file. I see the filename being posted to the database, but the file is not being posted to the images folder. It seems as though nothing is happening. Here is my following code, please advise me what I need to change:

<?php
//the $art variable gets posted to a database eventually
    $art = mysql_real_escape_string(stripslashes($_FILES["art"]["name"]));
$art_ext = pathinfo($art, PATHINFO_EXTENSION);
$art = md5($art).".".$art_ext;
if($art!=""){
    move_uploaded_file($art, "images/".$art );
}
?>
<script type="text/javascript">
$(function(){
    image_upload = {
        UpdatePreview: function(obj){
          // if IE < 10 doesn't support FileReader
          if(!window.FileReader){
             // don't know how to proceed to assign src to image tag
          } else {
             var reader = new FileReader();
             var target = null;

             reader.onload = function(e) {
              target =  e.target || e.srcElement;
               $("#imageupload").attr("src", target.result);
             };
              reader.readAsDataURL(obj.files[0]);
          }
        }
    };
});
    </script>
    <form action="new.php" method="post" enctype="multipart/form-data">
<input type='file' name='art' id="file" onchange='image_upload.UpdatePreview(this)' value="Upload"  accept="image/gif,image/jpeg,image/png"/>
        </p>
        <p>upload a image! (.gif, .jpg, .png formats allowed. 5MB max)</p>
        <img id="imageupload" src="1x1.png" alt="test" />
<input type="submit" class="smallbtn4" style="cursor:pointer;" value="post"/>
</form>
1
  • 1
    It looks to me to be a problem with the filename. The 1st var for the move_uploaded_file function is the actual name of the uploaded file - you can't md5 that first. What you probably intended to do was save it with an md5 hashed name. Reviewing the function's definition, we see the following: bool move_uploaded_file ( string $filename , string $destination ) - I suggest you find and download 'php_enhanced_en.chm' if you're an english speaker. It's the php help-file, with the enhancement of user-comments from the PHP site. It's about 29MB (it contains 1000's of code snippets) Commented Aug 28, 2013 at 22:55

2 Answers 2

1

Here's the format for move_uploaded_file():

$path = 'images/';
move_uploaded_file($_FILES['art']['tmp_name'], $path.basename($_FILES['art']['name']));
Sign up to request clarification or add additional context in comments.

2 Comments

AWESOME! I did the following move_uploaded_file($_FILES['art']['tmp_name'], "images/".$art );
If you use my library at PHPglue.com it could make your life a lot easier.
1

when using move_uploaded_files() your destination path should also include the name of the file....right now your destination path is:

images/

it should be:

images/nameOfImg.ext

Hope this helps!

EDIT:

After seeing the comment by @enhzflep, you should also hash the name of the file and create your filename string before using it in move_uploaded_file();

1 Comment

Okay so I changed a couple things around, I actually see the file uploading but I still don't see it in the file path. $art = mysql_real_escape_string(stripslashes($_FILES["art"]["name"])); $art_ext = pathinfo($art, PATHINFO_EXTENSION); $art = md5($art).".".$art_ext; if($art!=""){ move_uploaded_file($art, "images/".$art ); }

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.