0

A weird problem when i am trying to make a banner art upload system for user profiles.

I have searched for answers, but none have solved it.

It is not a permissions problem, my chmod is 755 on uploads folder and in the upload.php file.

code below:

if ($_POST && !empty($_FILES)) {
$formOk = true;

//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];

if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
    $formOk = false;
    echo "Error: Error in uploading file. Please try again.";
}

//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/gif'))) {
    $formOk = false;
    echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
    $formOk = false;
    echo "Error: File size must be less than 3 MB.";
}

if ($formOk) {
    // read file contents
    $content = file_get_contents($path);

    //connect to mysql database
    if ($conn = mysqli_connect('localhost', 'hidden', 'hidden', 'hidden')) {
        $user_id = mysqli_real_escape_string($conn, $_POST['user_id']);
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $content = mysqli_real_escape_string($conn, $content);
    $sql = "insert into banners (user_id, username, name, size, type, content) values ('{$user_id}','{$username}','{$name}', '{$size}', '{$type}', '{$content}')";

        if (mysqli_query($conn, $sql)) {
            $uploadOk = true;
            $imageId = mysqli_insert_id($conn);
        } else {
            echo "Error: Could not save the data to mysql database. Please try again.";
        }

        mysqli_close($conn);
    } else {
        echo "Error: Could not connect to mysql database. Please try again.";
    }
}

And the form is:

        <form method="post" enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" >
      <div>
        <h3>Image Upload:</h3>
      </div>
      <div>
        <label>Image</label>
        <img src="<?php print_r($path); ?>"/>
        <?php print_r($_FILES); ?>
        <input type="text" name="user_id" value="<?php echo $user->data["user_id"]; ?>" readonly/>
        <br />
        <input type="text" name="username" value="<?php echo $user->data["username"]; ?>" readonly/>            
        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
        <input type="file" name="image" />
        <input name="submit" type="submit" value="Upload">
      </div>
    </form>     

I have the form in the same file upload.php along with my include header and footer.

So basically it stores the data into the database, but just will not upload what so ever, i also checked the phpinfo and so on, and all is ok, it is turned on, and file limit in phpinfo is 64mb, so i don't get why it wont upload?

any help would be appreciated, thanks

6
  • doesn't upload "where" exactly? In a folder? I don't see move_uploaded_file() anywhere. "on uploads folder" - where's that folder declaration? May 20, 2016 at 18:24
  • I searched twice for move_uploaded_file()
    – Amit Ray
    May 20, 2016 at 18:26
  • "A weird problem" - there's nothing "weird" here, it's what's "missing" and what you're not "doing" and that is, "moving" the file to the folder php.net/manual/en/function.move-uploaded-file.php - Now let's all wait for it................. I should making this an answer instead. But I sense the sun is starting to go down somewhere. May 20, 2016 at 18:27
  • Check this out w3schools.com/php/php_file_upload.asp
    – Amit Ray
    May 20, 2016 at 18:31
  • Sorry everyone for late reply, i had to go out. i will try this now what people have suggested with the move file snippets. i will post an update if this was why it wasnt uploading, thanks all May 20, 2016 at 19:27

2 Answers 2

1

Simple: You didn't move that file anywhere, as in move_uploaded_file().

Base yourself on the following example taken from the manual:

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

and check for errors too.

7
  • ok thanks, i did look at the manual and also on the w3school site too, and i even copied code from there and tweaked it, and that code had this in it, but it still didnt upload then either!, but i will try again and post a reply, if this isnt the fix. May 20, 2016 at 19:28
  • Ok so yes the file uploads now, i am just annoyed, i tried this method and it kept doing the same, not uploading, now all of a sudden it uploads. thanks everyone May 20, 2016 at 19:46
  • So now it isn't adding it to the database, but images are uploading? May 20, 2016 at 20:02
  • @TubeNations Hi, sorry I had left for a while and I'm back now. I'm glad to hear that it all worked for you, cheers May 20, 2016 at 21:00
  • Thanks Fred, i am still learning PHP, i had a coder who did some parts to my site, but now that coder can no longer code for me, so now i had to learn myself. but i am getting better all the time, im just a slow learner :) May 21, 2016 at 20:51
1

I just wanted to post how i did it, thanks to Fred and some other comments others made :)

                //Assign Variables
            $username = $user->data['username'];
            #mkdir("uploads/".$username,0755);
            $target_dir = "../uploads/$username-";
            $path = $target_dir . basename($_FILES['image']['tmp_name']);
            $name = $target_dir . basename($_FILES['image']['name'].$user_id);
            $size = $_FILES['image']['size'];
            $type = $_FILES['image']['type'];



                if (move_uploaded_file($_FILES['image']['tmp_name'], $name)) {
            echo "<h2>Thank you for uploading your banner art - File is valid, and was successfully uploaded.\n</h2>";
        } else {
            echo "Possible file upload attack!\n";
        }

so I also made it so it now puts a username in front of the uploaded images name too.

The form is pretty much the same, I now also made it so that the upload and update open in a nice fancy box :)

https://gyazo.com/e12a03ce01d8f503b8fa2e79f5605809

Thanks again everyone :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.