0

Right now I am studying how to upload a photo using PHP. I copied a code from w3schools.com but I can't guess where I should change the name of photo. This code uploads photo with the name it had before, but I want to change it so where I should write it in ?

<?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.";
    }
}
?>

5
  • One thing to sucesfully learn any language is to understand how the examples work, not just copy pasting it together. If you understood this example, you would realize it is only matter of changing one variable value, Commented Oct 11, 2016 at 11:00
  • Take a short look at the documentation and you have your answer: php.net/manual/de/function.move-uploaded-file.php Commented Oct 11, 2016 at 11:00
  • I've solved this problem some months ago. I rename the photos to the timestamp they were uploaded. Did it this way: $newfilename = round(microtime(true)) . '.' . end($temp); move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "some/folders/maybe/" . $newurlname); Commented Oct 11, 2016 at 11:00
  • guys I couldn't. for example if i write $target_file = $target_dir . "name" . "jpg"; it gives me error like:"Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded." Commented Oct 11, 2016 at 11:52
  • // 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; } Commented Jun 17, 2018 at 13:54

3 Answers 3

1

You change the file name in this line:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

Do something like

$target_file = $target_dir . "heyyyyy." . $imageFileType;
Sign up to request clarification or add additional context in comments.

4 Comments

$target_file = $target_dir . "you new file name goes here";
exactly what I did, but you also need to insert file extension
I have a problem with file extension. in case of:"$target_file = $target_dir . "name" . $imageFileType; it gives me :"Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded."
What is the extension of the file you're trying to upload?
1

In order to ensure the extension is lowercase, Replace:

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

With:

$imageFileType = strtolower(pathinfo($original_file,PATHINFO_EXTENSION));

Comments

0
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" />
    <button type="submit">Upload</button>
    </form>

Here is the file to upload: name="fileToUpload". This should resolve it.

Comments

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.