www.webdeveloper.com
Results 1 to 7 of 7

Thread: File system upload error?

  1. #1
    Join Date
    Apr 2014
    Posts
    42

    File system upload error?

    For some reason I get the "your file is wrong" error when I run this. I'm trying to get it so that the values go to the database, and the image id goes to a folder on my server, so that I can recall the images when I want to see them on the homepage. Thanks

    Code:
    if (isset($_POST['upload'])) {
            if (((@$_FILES["image"]["type"]=="image/jpeg") || (@$_FILES["image"]["type"]=="image/png") || (@$_FILES["image"]["type"]=="image/jpg") || (@$_FILES["image"]["type"]=="image/gif"))&&(@$_FILES["image"]["size"] < 2097152)) //2 Megabytes
      {
       $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
       $rand_dir_name = substr(str_shuffle($chars), 0, 15);
       mkdir("userdata/user_photos/$rand_dir_name");
    
       if (file_exists("photos/$rand_dir_name/".@$_FILES["image"]["name"]))
       {
        echo @$_FILES["image"]["name"]." Already exists";
       }
       else
       {
        move_uploaded_file(@$_FILES["image"]["tmp_name"],"photos/$rand_dir_name/".$_FILES["image"]["name"]);

  2. #2
    Join Date
    Apr 2014
    Posts
    42
    Sorry, idk why this thing cut part of my code out, here's the rest of it...


    Code:
    $username = 'ralston3';
    	$description = $_POST['description'];
    	$image_name = @$_FILES['image']['name'];
    	$image_id_before_md5 = $rand_dir_name/$image_name;
    	$image_id = md5($image_id_before_md5);
    	$date = 'Y-m-d h:i:sa';
    	
    	$sqli = "INSERT INTO `photos` (username, description, image_name, image_id, date) VALUES ('ralston3','$description','$image_name','$image_id','$date')";
    	
    	mysqli_query($conn, $sqli) or die ("No query");
    	
    	header('location:index.php');
    	
    	
        //$profile_pic_name = @$_FILES["profilepic"]["name"];
        //$img_id_before_md5 = "$rand_dir_name/$profile_pic_name";
        //$img_id = md5($img_id_before_md5);
        //$profile_pic_query = mysql_query("INSERT INTO photos VALUES ('','test','$user','$date','$description','http://localhost/tutorials/findFriends/userdata/user_photos/$rand_dir_name/$profile_pic_name','no','$img_id')");
        //header("Location: upload_photo.php");
    
      			 }
    		} else echo "Your file is wrong";
    	}
    ?>

  3. #3
    Join Date
    May 2014
    Posts
    899
    It looks like $_POST['upload'] isn't set... I'd have to see the form in question, but I suspect you are checking a $_POST value that doesn't actually exist -- since input[file] doesn't MAKE a $_POST variable.

    I suspect that this:
    if (isset($_POST['upload'])) {

    Should be this:
    if ($_FILES['upload']['error'] == 0) {

    But that's guessing wildly without seeing the form that's being submit.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Apr 2014
    Posts
    42
    here is the submitted form:

    Code:
            	<form action="posts_check.php" method="POST" name="posts_form" id="posts_form" onSubmit="return validateFrom()" enctype="multipart/form-data">
                <input type="file" name="image" id="image"><br>
                <textarea name="description" id="description" placeholder="Enter a description of your work here...";></textarea><br>
                <input type="submit" name="upload" id="upload" value="Upload">
                </form>

  5. #5
    Join Date
    May 2014
    Posts
    899
    Ok, you seem to be unnecessarily looking for the submit -- which, laughably, your trapping 'onsubmit' may actually be screwing with. I would check if the file exists as I said above, or a hidden input for checking the form, instead of the submit and it's value.

    Of course, I'd also swing a giant axe at that idiotic MESS of error overrides. IF you have to override an error with the @ sign, you are doing things WRONG.

    That said, your lack of sensible formatting had me backtracking to the wrong if statement -- the one with all those error overrides is the one that's failing.... and part of that could be that ['type'] is an array, so if there's just one upload it should be ['type'][0]...

    OH, and you shouldn't rely on ['size'] actually existing, not all servers report it on uploads.

    ASSUMING ['size'] works...

    Code:
    	if (
    		isset($_FILES['image']) && 
    		($_FILES['image']['size'] < 2097152) && 
    		(in_array($_FILES['image']['type'], array(
    			'image/jpeg', 'image/png', 'image/jpg', 'image/gif'
    		)))
    	) {
    MIGHT work, but really I'd do a

    echo '<pre>', print_r($_FILES), '</pre>';

    First to make sure all those values you are trying to check even exist.
    Java is to JavaScript as Ham is to Hamburger.

  6. #6
    Join Date
    Apr 2014
    Posts
    42
    Thanks for that, worked great! Everything was updated to the database. However, the image_id wasn't sent to the 'photos' folder on the server, where I'd like to store my images for recall later. Any idea why? All the code is the same (except for the part you updated).

  7. #7
    Join Date
    Apr 2014
    Posts
    42
    nevermind.figured it out.Thanks again for the help @deathshadow

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles