0

I have an error. I made an file upload in HTML 5 and PHP. When I try to upload anything there always appears an error. Here's the code:

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

upload.php:

$target_path = "upload/";
$target_path = $target_path . basename($_FILES["fileToUpload"]["name"]);

if(isset($_POST["submit"])) {
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_path)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been                                    uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
4
  • where do you initially define $target_file that is used in the if( file_exists( $target_file ) ){ ? I think you need to use $target_path in there! Also, you should include the error in your question rather than just "there's always an error" Commented Dec 29, 2015 at 8:58
  • Please follow the link i hope it will useful for you. stackoverflow.com/questions/1673393/php-file-upload Commented Dec 29, 2015 at 9:30
  • Just change the filename in the target portion of the move_uploaded_file call if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) Commented Dec 29, 2015 at 9:36
  • Still doesn't work. Maybe is't about my server on RPI, but I don't think so Commented Dec 29, 2015 at 10:47

2 Answers 2

0

As mentioned in the comment, the variable used in the logic test was not defined anywhere. I believe you were intending something like this.

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

    $target_path = "upload/";
    $target_file = $target_path . basename( $_FILES["fileToUpload"]["name"] );

    if( isset($_POST["submit"]) ) {

        if ( file_exists( $target_file ) ) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        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.";
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Just change the filename in the target portion of the move_uploaded_file call

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))

1 Comment

Still doesn't work. Maybe is't about my server on RPI, but I don't think so.

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.