Question

I created a simple php page to upload a file but, it does not work. I the message ok but if I enter the folder "prova" there is nothing... The folder is set to 777 here there is the code

<html>
<head>
   ...
</head>
<body>
    <form action="importazione.php?a=1" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="Aggiorna" value="Submit">
    </form>
    <?php
    $messaggio= $_GET['a'];
    define("UPLOAD_DIR", "/home/me/Scrivania/prova");
    if ($messaggio == 1)
    {
        $allowedExts = array("txt");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ( $temp[1] == "txt") {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } 
            else {
                if(isset($_FILES['file']))
                {
                    $file = $_FILES['file'];
                    if($file['error'] == UPLOAD_ERR_OK and is_uploaded_file($file['tmp_name']))
                    {
                        move_uploaded_file($file['tmp_name'], UPLOAD_DIR.$file['name']);
                        echo "OK";
                    }
                }
            }
        } 
        else {
            echo "Invalid file";
        }
    }
    ?>
</body>

Was it helpful?

Solution

Change this:

define("UPLOAD_DIR", "/home/me/Scrivania/prova");

To this:

define("UPLOAD_DIR", "/home/me/Scrivania/prova/");

Tested your script on my server and works fine, just needed that extra slash on the end, because the url to where the file is to be uploaded to is translates to:

/home/me/Scrivania/provafile.txt

when it should be

/home/me/Scrivania/prova/file.txt

-- Edit --

Also if you intend on using the ext array change the if statement to the following:

if ( in_array($temp[1], $allowedExts) ) {

OTHER TIPS

Change this

move_uploaded_file($file['tmp_name'], UPLOAD_DIR.$file['name']);

to this

move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_DIR.'/'.$_FILES['file']['name']);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top