[RESOLVED] php upload file warning message
Results 1 to 7 of 7

Thread: [RESOLVED] php upload file warning message

  1. #1
    Member
    Join Date
    Jan 2017
    Location
    Michigan / United States
    Posts
    32

    resolved [RESOLVED] php upload file warning message

    When I run this php file it executes everything except the move_uploaded_file. It produces the following Warnings:

    warning: move_uploaded_file(uploadsFlowers002.JPG<br>): failed to open stream: Invalid argument in C:\xampp\htdocs\tutorials\filehandeling\upload.php on line 20

    Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php43D.tmp' to 'uploadsFlowers002.JPG<br>' in C:\xampp\htdocs\tutorials\filehandeling\upload.php on line 20

    The php file location is C:\xampp\htdocs\tutorials\filehandeling.
    The move to location is C:\xampp\htdocs\tutorials\filehandeling\uploads.


    I think **$location****$tmp_name****$name** are valid declared variables.
    So, I don't understand invalid argument on line 20.

    I suspect this problem is due to trying to move selected file to the uploads folder.
    I've tried the following: $location=C:\xampp\htdocs\tutorials\filehandeling\uploads
    $location=C:\xampp\htdocs\tutorials\filehandeling
    $location='\uploads';
    $location='/uploads';
    $location='uploads\';
    $location='uploads/';
    $location='uploads';

    I've also tested with different file type, such as: .txt and got the same warning

    Don't know what else to do.
    PHP Code:
     <html>
        <form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type ="file" name ="file"><br><br>
        <input type ="submit" value="submit">
        </form>
        </html>

        <?php
    echo 'File name:&nbsp&nbsp&nbsp'$name $_FILES ['file'] ['name'].'<br>';
    echo 
    'File size:&nbsp&nbsp&nbsp'$size $_FILES ['file'] ['size'].'&nbsp'.'kb'.'<br>'
    echo 
    'File type:&nbsp&nbsp&nbsp'$type $_FILES ['file'] ['type'].'<br>';
    echo 
    'Temporary file location:&nbsp&nbsp&nbsp',$tmp_name =$_FILES ['file'] ['tmp_name'];

    if(isset(
    $name)) {
        if (!empty(
    $name)) {
            
        
    $location 'uploads';
        
        
        if (
    move_uploaded_file($tmp_name$location$name)) {
            echo 
    '<br>File that was <strong><font color="red">UPLOADED</font></strong>';
        }    
        }    else {
            echo 
    'Please choose a file';
        }
    }


    ?>
    Any help would be greatly appreciated.
    Last edited by NogDog; 01-27-2017 at 11:03 PM. Reason: added [php] tags around code

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,692
    PHP is pretty smart about dealing with directory separators in Windows, so you should be able to designate the destination directory with "/" separators (which means you don't have to worry about "\" characters being treated as escape characters), and it will know that they're really "\" in Windows. Also, don't forget you'll need a slash between the directory and the file name. Also, assigning elements of the $_FILES array to new scalar variables doesn't really gain you anything (and wastes a tiny amount of resources), so I wouldn't bother, instead doing something like:
    PHP Code:
    $uploadDir 'C:/xampp/htdocs/tutorials/filehandeling/uploads/';
    if(!empty(
    $_FILES['file']['tmp_name'])) {
        if(
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir $_FILES['file']['name']) {
            
    // success message
        
    }
        else {
            
    // error message
        
    }

    (Untested!)
    Last edited by NogDog; 01-27-2017 at 11:22 PM.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,409
    You would probably have more success if you didn't try to give the file a name ending with "<br>"; '<' and '>' are redirection operators and can't appear in filenames.

    This is alluded to in the second error message where it says that the file couldn't be moved to "uploadsFlowers002.JPG<br>".
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Member
    Join Date
    Jan 2017
    Location
    Michigan / United States
    Posts
    32
    NogDog.
    When I tested the script you provided there was 1 parse error on line 3 of the script. Omitted " ) " after ['name'].
    When I corrected parse error this solution worked perferctly.
    Many thanks.
    I was able to sleep last night.

  5. #5
    Member
    Join Date
    Jan 2017
    Location
    Michigan / United States
    Posts
    32
    Weedpacket.
    Thank you for pointing out the <br> in the error message.
    However the file name does not include <br> when I check the file properties.
    Is it possible that when I run the script it is appending the <br>?
    The only possible place I can see that happening is on line 8. There is a " . " <br> after ['name']
    echo 'File name:&nbsp&nbsp&nbsp', $name = $_FILES ['file'] ['name'].'<br>';

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,409
    Yes, that is where it is happening.
    You could replace the concatenation operator (.) with a comma, as you do elsewhere on the line, but it would be safer to do the assignment and the output as separate statements.
    Last edited by Weedpacket; 01-28-2017 at 05:53 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #7
    Junior Member
    Join Date
    Feb 2017
    Posts
    1
    I found one Best Tutorial for PHP FIle Upload with Preview It was very Helpful Give a Try Once It was very Helpful.
    Thank YOu

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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