Community discussion forum

Uploading files in PHP

This is a comment thread discussing Uploading files in PHP
  • 9 years ago

    This thread is for discussions of Uploading files in PHP.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 6 years ago

    when a partner wrote a simple script to receive uploaded files, the owner of the file was set to nobody, which is the apache webserver, I believe.
    In this case, we can't access or manipulate the uploaded files via ftp or the shell.  Chmod doesn't seem to work.


    Are we doing something wrong or is this always the case?


    Thanks,
    Collin

  • 5 years ago

    Hi,
    I'm a new inexpert php programmer.


    I've tested your script upload.html and upload.php and it is all  ok.


    Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.



    test 1)
    I try to substitute in upload.htm the line:
    :---------
      submit this file: <input type=file name="userfile"><br>
    :---------
    with this line :
    :---------
      submit this file: <input type="hidden" name="userfile" value="cippo.ttt"><br>
    :---------
    ... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
    :---------
    Warning: Unable to open 'cippo.ttt' for reading: No such file or directory in your script on line 12
    :---------





    test 2)
    to debug I add a line in upload.php
    :---------
       echo "$userfile";
       echo "<br>-xxx-<br>";
    :---------
    ... but the server replay:


    :---------
    ./phpIV1Asq
    -xxx-


    Warning: Unable to open 'cippo.ttt' for reading: No such file or directory in your script on line 9
    :---------






    The question:


    Why the string cippo.ttt is echoed as ./phpIV1As ?


    Thank you for your help.


    Best regards

  • 5 years ago

    Hi,
    I'm a new inexpert php programmer.


    I've tested your script upload.html and upload.php and it is all  ok.


    Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.



    test 1)
    I try to substitute in upload.htm the line:
    :---------
      submit this file: <input type=file name="userfile"><br>
    :---------
    with this line :
    :---------
      submit this file: <input type="hidden" name="userfile" value="c:pippo.ttt"><br>
    :---------
    ... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
    :---------
    Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 12
    :---------





    test 2)
    to debug I add a line in upload.php
    :---------
       echo "$userfile";
       echo "<br>-xxx-<br>";
    :---------
    ... but the server replay:


    :---------
    ./phpIV1Asq
    -xxx-


    Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 9
    :---------






    The question:


    Why the string c:pippo.ttt is echoed as ./phpIV1As ?


    Thank you for your help.


    Best regards

  • 5 years ago

    I am new php programmer but when I test your upload php. I got a message


    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
    file uploaded


    instead. what should I do to solve this prob?

  • 5 years ago

    Try on a php supported server (i.e. sign up on www.free.fr)  
    bye

  • 5 years ago

    Hi


    Run your application from the "public" folder under your server's root directory.


    S.S

  • 5 years ago

    are you talking to me?

  • 5 years ago

    Hi!


    I used this code for my upload form and worked ok...there's just something i can't get to work.
    I placed a check for the "empty input field" and I can't get it to work.


    <html>
    <?
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>


    No matter if I turn the code "upside down", it only does the "if" part..can you help me?


    -meissi-

  • 5 years ago

    That's so easy in my self server, but I am using free hosting like iwebland.com.
    May I change temporary directory? no I think so.
    I tried do a copy inside my root, and I discover that I haven't permision to do that.
    I don't know java.
    I can't use ftp because I can't give the access to my customer, but he need put images in the database.
    May redirect the upload trought "<input type=file..." to the directory in my root?

  • 5 years ago

    I have a little prob
    I am new php programmer but when I test your upload php. I got a message


    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
    file up...


    You need to make sure the directory you are uploading to has write permissions. If it doesn't you will get that error!

  • 5 years ago

    I have had this problem before.  Your code appears to be ok so it seems odd that it is not working.  Try using the same code but without the braces {} - this often helps me in PHP when for some reason the second branch of the conditional is never reached.




    Quote:
    [1]Posted by meissi on 26 Feb 2003 06:32 AM[/1]
    Hi!


    I used this code for my upload form and worked ok...there's just something i can't get to work.
    I placed a check for the "empty input field" and I can't get it to work.


    <html>
    <?
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>


    No matter if I turn the code "upside down", it only does the "if" part..can you help me?


    -meissi-

  • 5 years ago

    Quote:
    ...I am new php programmer but when I test your upload php. I got a message


    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upl



    bonk on head


    Perhaps you don't have a www/phptest/ folder


    The text "'/home/www/phptest/test.txt" was just an example, you can make it anything you want, as long as its a valid place in your webspace.

  • 5 years ago

    "./phpIV1As" sounds like a directory name, probably the directory of your php folder or where your files are stored on your webspace, which may be inaccessable to you.


    With your "c:pippo.ttt" file name, have you tried putting in the "\" backspace after C: in this text? If you don't, which is what you have done here, then PHP will look for the pippo.ttt file in the present working directory of the C: Drive, which is unnecessarily the root folder of the C: drive.

  • 5 years ago

    Perhaps it's more likely that the data getting put into $userfile is not working properly leaving it empty.


    I'd also rather have "<?php" instead of just "<?" when starting to write php code.


    <?php


    if(!empty($userfile))
    {
     copy($userfile, "kuva.gif");
     echo("file uploaded");
    }
    else
    {
     echo("FAILED!");
    }


    ?>

  • 5 years ago
  • 5 years ago

    Hello, I have tried using your script and I have checked over my code for syntax and it appears to be the exact same as yours. When I test it, it appears to not upload anything and skips right to the else statement "FAILED". It appears as username is not being passed properly to the php page. Also  copy($userfile, "kuva.gif"); that line what should the value in " " be. THe path where the uploaded file is going to be stored? I dont understand how someone could have a .gif. Plz help
    HTML PAGE


    <html>
    <form action="myupload.php" method=post enctype="multipart/form-data">
    submit this file: <input type=file name="userfile"><br>
     <input type=submit>
     <br>
    </form>
    </html>


    PHP


    <html>
    <?php
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>

  • 5 years ago

    This is due to a change in the way PHP lets you access POST'ed variables. I've updated the article to reflect these changes.

  • 5 years ago

    Sorry about the delayed reply! Try this:


    <?php


    if(!empty($userfile))
    {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    elseif (empty($userfile))
    {
    echo("FAILED!");
    }


    ?>

  • 5 years ago

    i


    when execute that script the value of $FILES["userfile"]["tmpname"], $FILES["userfile"]["type"], and $FILES["userfile"]["size"] are empty,
    the value of $_FILES["userfile"]["name"], yes, take the name of the file to upload, but, don't make nothing, why ?




    thank's in advance

  • 4 years ago

    I seem to have the same problem as someone in an earlier post.  I don't seem to have my directory correct.  Here is the portion of my code that specifies the directory:


    moveuploadedfile($FILES['userfile']['tmpname'], "home/successa/publichtml/".$FILES['userfile']['name']);


    When I run it, I get:


    Warning: moveuploadedfile(home/successa/publichtml/NTUSER.DAT.LOG): failed to open stream: No such file or directory in /home/successa/publichtml/processfile.php on line 15


    Warning: moveuploadedfile(): Unable to move '/tmp/php6fQXnx' to 'home/successa/publichtml/NTUSER.DAT.LOG' in /home/successa/publichtml/processfile.php on line 15


    I don't know how to specify the directory.  When I log into my control panel, whatever root directory I am in, I see a publichtml folder there.  I've tried just specifying "/publichtml" as the directory but that doesn't work either!  I read someplace that it should be the "absolute location" but in this case I'm not sure what that means.  I've checked for write permission and I do have that...???  I feel so dumb!    

  • 4 years ago

    Hello, I am new to PHP and have done a little bit of research into uploading files. I believe you need to have a 'hidden' field in the form called 'MAXFILESIZE' with a value no greater than 2000000(that's the default in most PHP configs). I hope this helps!


    Nick

  • 4 years ago

    Try $FILES['userfile']["tmpname"] ... I even had to 'waste' the single quote in a script where the var userfile was variable itself.


    Steven

  • 4 years ago

    You might want to replace the double quotes in the second [] too ...


    Steven

  • 4 years ago

     This error message is common when the upload directory does not have the right permissions for the web server to write to it.


     You must change the write permissions of the upload directory allowing the web server to write to it.

  • 3 years ago

    Are there C functions equivalent to those in php which will allow
    me to write the upload.php in C? I have a lot of form processing
    that is all written in C, and I would like to integrate the uploading
    of files right into other procedures.
    Thanks for you help.
    John

  • 3 years ago

    i have done everything you have said and this error comes up and cant figure out what this means right now


    Parse error: parse error, unexpected T_IF in c:\appserv\www\upload.php on line 7


    Thanks for any reply

  • 3 years ago

    The problem is a missing ";"


    add a semi colon after:


    $uploaddir = "/upload/"


  • 3 years ago

    The T_IF error is the missing ";" at the end of the $uploaddir = "/path" statement in his code.  In my case the code simply isn't working and I'm not sure why.  The file just doesn't upload.

  • 3 years ago

    You should add quotation marks to the upload.htm script as follows:


    method=post change to method="post"
    type=file change to type="file"
    type=submit change to type="submit"


    Also note - The php manual  recommends you include max file size, which appearently warns the user before the upload begins.


    btw the script worked fine for me after these corrections


    my site: carterettechservice.com

  • 3 years ago

    how can i get the exact address of the uploaded file i browsed.


    like when i browsed it from "C:/picture.bmp"


    how can i get this address? i mean how could i get these exact words?


    and when i upload this picture to the server, i declared the address of the server.
    but then it says, "permission denied"


    what does it mean?

  • 3 years ago

    Hi,


    I'm relatively new to php. I'm using a code simliar to the one posted for file uploads. I was wondering if there was any way to set the permisson to "read only" upon upload. I've been having problems with uploading images that will only display if I manually set the permission in my site admin tool. Any help would be muck appreciated.
    Thank you,
    Mike

  • 3 years ago

    gud day!
    i have problems regarding the file i uploaded on the server.


    the file is pic.bmp


    i uploaded it in "subs/images"
    then i am about to create an exec statement to convert this logo.


    its permission is "-rw-------" and i would like to change it.
    i typed "chmod 777 pic.bmp"


    it says "operation not permitted"
    what can i do?

  • 3 years ago

    Is there anyway to change the filename as it is uploaded?   So  myhouse.jpg   is auto changed to file1.jpg


    For instance, anytime a file is uploaded via the form... Have it name the file   MYFILE.jpg


    so that when a new file is uploaded it overwrites the old file without having to have the user specify the correct filename before uploading it.

  • 2 years ago

    I am trying to allow people to upload image files and post them to me.

     

    I have copied your code exactly but get the following;

    Parse error: parse error, unexpected T_IF in /nfs1/expatriatespain.com/www/test site/myupload.php on line 16

    Any ideas?

     

Post a reply

Enter your message below

Sign in or Join us (it's free).