0

I am new to php, hence cannot figure out my problem. I have an HTML form that is connected to the mentioned php file. This form works fine. The problem is with the php file. The HTML form is:

    <!DOCTYPE html>
    <html>
    <body>

    <form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
    </form>

    </body>
    </html>

And my php file called upload.php is:

    <?php
    $uid = $_POST['uid'];
    $target_dir = "FILES OF:$uid";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
    } else {
    echo "File is not an image.";
    $uploadOk = 0;
    }
    }
    ?>

Here, as you can see in the first lines of the php code, the target directory is set. But I want it to be with a variable called

    $uid

which is linked to the uid field in my database meaning username. What I mean to say is that I want it to upload files in the folder called "FILES OF:$uid". I have these folders already created but want a way in which files will go directly to that folder. Does anyone know a way to do this?

  • If you want different folder for each user u must use database and login so when user login, you get user id and you can make dir based on that user id. With this approach you can't because `$_POST` variable reads name from a input of a form, and you don't have any username in there. – Mario Mar 22 '17 at 20:17

1 Answers1

1

hi i have link where you can check your code is correct or not if you don't want to check link then check following code.

image upload to specific folder

following code is copy from given link

 <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>


  [1]: https://www.w3schools.com/php/php_file_upload.asp
Dipen Soni
  • 224
  • 2
  • 8
  • You can't rely on mime type, its better to check for image extension. – Mario Mar 22 '17 at 20:11
  • no i can trust on mime type and it's work for me and i think you also use mine type. @Mario – Dipen Soni Mar 22 '17 at 20:19
  • Please read this before you trust :D http://stackoverflow.com/questions/7349473/php-file-upload-mime-or-extension-based-verification – Mario Mar 22 '17 at 20:27
  • thank you, but I already have these codes. I wanted to know if there was a way in which I could set a variable instead of uploads/ in target directory. – Archit The Great Mar 23 '17 at 04:45