Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys i' m trying to get this to work. But i haven't been able yet to upload a file to my uploads folder. If i echo the $tmp_name = ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47

    problem with file upload in php


    Hi guys i' m trying to get this to work.
    But i haven't been able yet to upload a file to my uploads folder.
    If i echo the $tmp_name = $_FILES['file']['tmp_name']; i see that the file goes to the temp location. anybody have any suggestion why this doesnt work I have set up my environment so that i have virtual hosts on my Apache2. Nor does it echo that the file has been uploaded.

    When i look in the phpinfo file i notice that
    the setting for Virtual Directory Support is disabled could this have an effect on the file uploads.

    Thanks in advance guys..

    Here is the code

    <?php
    $name = $_FILES['file']['name'];
    //$size = $_FILES['file']['size'];
    //$type = $_FILES['file']['type'];
    $tmp_name = $_FILES['file']['tmp_name'];

    //$error = $_FILES['file']['error'];

    if (isset($name)) {
    if (!empty($name)) {

    $location = 'uploads/';

    if (move_uploaded_file($tmp_name, $location.$name)) {
    echo 'Uploaded!';
    }

    } else {
    echo 'Please choose a file.';
    }
    }

    ?>


    <form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="submit">

    </form>

  2. #2
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Are you getting any error messages?

    Does the folder "uploads/" exists?

    You may wanna try:
    PHP Code:
    <?php


    //$error = $_FILES['file']['error'];

    if (isset($_FILES['file']['name'])) if ($_FILES['file']['name'] != "") {

    $name $_FILES['file']['name'];
    $tmp_name $_FILES['file']['tmp_name'];

    $location 'uploads/';
    if (!
    chdir($location))
    if (!
    mkdir($location"777"true))
    echo 
    "Error - Folder \"$location\" doesn't exist and couldn't be created!<br>\n";

    if (
    move_uploaded_file($_FILES['file']['tmp_name'], $location.$name)) {
    echo 
    'Uploaded!';
    } else {
    echo 
    "File \"$location$name\" couldn't be created!<br>\n";
    }
    } else {
    ?>
    Please choose a file:
    <form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="submit">
    </form> 
    <?php ?>

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47
    thanks Zombikillah. I will try it. and let you know how it went.
    Yes the uploads folder exists. No i don't receive any error messages. ( I have changed did change the setting for error reporting at sometime ). and now when i have started to develop more i will have to turn it on again.

    Thanks again for you time and input i will let you know how it went...

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47
    Hi there Zombikillah.
    I have tried your solution, with minor changes to the code. See below. I didnt get it to work. And i get this error message. File "uploads/ek.JPG" couldn't be created!
    Any solutions why i cant create the file ? Any one who knows perhaps. I have changes the default root directory on my apache and i have encrypted my harddrives. Dont know if that has anything to do with it but still, some backgroung info.

    <?php
    //$error = $_FILES['file']['error'];

    if (isset($_FILES['file']['name'])) if ($_FILES['file']['name'] != "") {

    $name = $_FILES['file']['name'];
    $tmp_name = $_FILES['file']['tmp_name'];

    $location = 'uploads/';
    if (!chdir($location))
    if (!mkdir($location, "777", true))
    echo "Error - Folder \"$location\" doesn't exist and couldn't be created!<br>\n";

    if (move_uploaded_file($_FILES['file']['tmp_name'], $location.$name)) {
    echo 'Uploaded!';
    } else {
    echo "File \"$location$name\" couldn't be created!<br>\n";
    }
    }
    ?>
    Please choose a file:
    <form action="uploads.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="submit">
    </form>

    Any suggestions that i could try ?
    I would also like to ha a secure solutions conserning file uploads.


    Thanks in advance guys

  6. #5
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Maybe the user running the script has no right to create a file there.
    If the script has created the folder it should have the permission set.

    If you pre-created the folder it's possible that the user "www-data" or "apache" or whatever has no right to write there.

  7. #6
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47
    Thanks Zombie for the quick answer, could be the cause. I will look into it. And see what i come up with.

  8. #7
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47
    Hi there again Elijah and everybody else ( hope you will see this )
    I would like to add to this structure some more directories such as uploads, scripts, css & images for starters.
    Where is the best place to put these folder ? in my opinion it would be under the document root for each site ( DocumentRoot /sites/site1/www/ )
    So far i havent been able to make the uploads work when i have had the directory uploads under document root.
    Do i need to alter the actual virtual hostfile for the site in question? To make it work ? Mayby add some information about the actual directory that i want to add.
    Below is my virtual host file for site1

    NameVirtualHost 127.0.0.1
    <VirtualHost 127.0.0.1>
    ServerAdmin webmaster@localhost
    ServerName site1

    DocumentRoot /sites/site1/www/
    <Directory />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    </Directory>

    ErrorLog /sites/site1/logs/error.log

    LogLevel warn

    CustomLog /sites/site1/logs/access.log combined
    ServerSignature On

    </VirtualHost>

    This is my php code for the file upload function.

    <?php
    //$error = $_FILES['file']['error'];

    if (isset($_FILES['file']['name'])) if ($_FILES['file']['name'] != "") {

    $name = $_FILES['file']['name'];
    $tmp_name = $_FILES['file']['tmp_name'];

    $location = 'uploads/';
    if (!chdir($location))
    if (!mkdir($location, "777", true))
    echo "Error - Folder \"$location\" doesn't exist and couldn't be created!<br>\n";

    if (move_uploaded_file($_FILES['file']['tmp_name'], $location.$name)) {
    echo 'Uploaded!';
    } else {
    echo "File \"$location$name\" couldn't be created!<br>\n";
    }
    }
    ?>
    Please choose a file:
    <form action="index.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="submit">
    </form>

    This is the error message i get. It echoes out File "uploads/ek.JPG" couldn't be created!

    thanks in advance guys..

  9. #8
    Just Joined!
    Join Date
    Oct 2011
    Posts
    47
    This is now solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •