SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast MrK2's Avatar
    Join Date
    Jul 2001
    Location
    British Columbia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File must be uploaded twice to appear?

    I'm using move_uploaded_file to upload files to my web server. At the same time, I create a text file with information about the upload, using the uploaded file's name. My problem is that when I run the script, the file itself doesn't appear on my webserver, but the text file that I'm creating comes out just fine. If I run the script again with the same file however, it uploads properly. Here is the code I'm using for the upload:
    PHP Code:
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./{$_FILES['userfile']['name']}"); 
    I was thinking that there might be a problem with creating the text file and uploading with the same script, so I moved the text file code to another page. No effect.

    Any idea what I can change so that the file will upload properly?

  2. #2
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got exactly the same code for a site that I'm building and after testing it, it works...

    The only difference is that I'm saving the filename into a database and when retrieving, I'm just calling <img src="http://domain.com/$filename">

    and it uploads the first time. This is the code I'm using that was provided to me by Dr Larry Pepper

    PHP Code:
    <?php

    if ($_FILES['userfile']['tmp_name'] == '' || $_FILES['userfile']['tmp_name'] == 'none')
    {
        echo 
    'No file uploaded!';
    }
    else
    {
        
    // Move the file to the current directory, with the original filename
        
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./{$_FILES['userfile']['name']}");
        echo 
    'Filename: '$_FILES['userfile']['name'];
    }

    ?>
    Does it show the file name when you try to upload a file?

  3. #3
    SitePoint Enthusiast MrK2's Avatar
    Join Date
    Jul 2001
    Location
    British Columbia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course we have the same code; I took mine from that other thread where he was helping you.

    Here's the entire upload.php code, other than the form (it's the else portion of this if statement):
    PHP Code:
    if (isset($submit)) {
        if (
    $_FILES['userfile']['tmp_name'] == '' || $_FILES['userfile']['tmp_name'] == 'none')
        {
            echo 
    'No file uploaded!';
        }
        else
        {
            
    // Move the file to the current directory, with the original filename
            
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./{$_FILES['userfile']['name']}");
            
            
    $fileinfo $_FILES['userfile']['name'];
            
    $file_to_open $fileinfo ".dat";
            
            echo 
    "The file was uploaded successfully.<br><br>";
            echo 
    'Filename: '$_FILES['userfile']['name'], '<br>Size: '$_FILES['userfile']['size'], ' bytes.';
            echo 
    "
            <br><br>You\'re almost done.  Click continue to complete the upload process.
            <form action=upload2.php method=POST>
            <input type=hidden name=file_to_open value=
    $file_to_open >
            <input type=hidden name=fileinfo value=
    $fileinfo >
            <input type=hidden name=submittedby value=
    $submittedby >
            <input type=hidden name=filedesc value=
    $filedesc >
            <input type=submit name=submit value=Continue>
            </form>
            "
    ;
        }

    It send some of the data to the next page in order to create the text file I mentioned. Yes, the name and file size appear correctly on the confirmation page, and the page takes quite a while to load (indicating that the file is being uploaded).

    Ugh, it sucks having no access to a db. :/

  4. #4
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is a question only Dr Larry Pepper can answer. After all I am a beginner and that code appears to be correct...

    You might like to PM him a link to this thread so he might be able to help you out like he did for me.

  5. #5
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't find anything wrong with this code. Maybe this upload2.php does something to uploaded file?
    (2B) or (not 2B) = FF

  6. #6
    SitePoint Enthusiast MrK2's Avatar
    Join Date
    Jul 2001
    Location
    British Columbia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only code contained on upload2.php is regarding the creation of the text (dat) file. (fopen, a few fwrites, fclose, etc.)

    Hmm, I just tried switching the whole move_uploaded_file() section to copy(), and it works perfectly. As far as I can tell, move_uploaded_file put the file into a temp directory, but never moved it out to the upload folder. By re-running the script, it found the file in the temp directory, and sent it where it needed to go. Maybe I'm wrong, just speculating.

    Oh well, at least it works. Thanks for the help guys.

  7. #7
    Apache Expert i_like_php's Avatar
    Join Date
    Nov 2001
    Location
    Dallas, Texas
    Posts
    1,342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by dragonhawk
    I've got exactly the same code for a site that I'm building and after testing it, it works...

    The only difference is that I'm saving the filename into a database and when retrieving, I'm just calling <img src="http://domain.com/$filename">

    and it uploads the first time. This is the code I'm using that was provided to me by Dr Larry Pepper

    PHP Code:
    <?php

    if ($_FILES['userfile']['tmp_name'] == '' || $_FILES['userfile']['tmp_name'] == 'none')
    {
        echo 
    'No file uploaded!';
    }
    else
    {
        
    // Move the file to the current directory, with the original filename
        
    move_uploaded_file($_FILES['userfile']['tmp_name'], "./{$_FILES['userfile']['name']}");
        echo 
    'Filename: '$_FILES['userfile']['name'];
    }

    ?>
    Does it show the file name when you try to upload a file?
    not to sound dumb but how does your script work?
    i love php

  8. #8
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by MrK2
    Hmm, I just tried switching the whole move_uploaded_file() section to copy(), and it works perfectly. As far as I can tell, move_uploaded_file put the file into a temp directory, but never moved it out to the upload folder. By re-running the script, it found the file in the temp directory, and sent it where it needed to go. Maybe I'm wrong, just speculating.
    wow, move_uploaded_file() to copy() is really weird. especially since move_uploaded_file() is the preferred way to move/copy uploaded files. since the $_FILES array works, you're obviously using a recent version of PHP. however, i'm just curious, what is the version?
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  9. #9
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by i_like_php


    not to sound dumb but how does your script work?
    Well, you'll need a form to call that file. The full details in this post:

    http://sitepointforums.com/showthrea...threadid=59379

  10. #10
    SitePoint Enthusiast MrK2's Avatar
    Join Date
    Jul 2001
    Location
    British Columbia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi LaRRY. Although I had an unending stream of problems on my home test server (PHP 4.0.6), I couldn't get it to work on a web server with PHP 4.1.1 either.


Bookmarks

Posting Permissions

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