www.webdeveloper.com
Results 1 to 10 of 10

Thread: file upload

  1. #1
    Join Date
    Mar 2010
    Posts
    28

    file upload

    hey guys,

    I have an html form to upload a file to be used and an external php script, as shown below, the problem is that when I try to pull the file into the script, it doesn't come up.

    HTML Code:
    <html>
    <body>
    <form action="upload.php" action="post" enctype="multipart/form-data">
    <label><input type="file" name="file" id="file"/> 
    <input type="submit" name="fileSubmit" id="fileSubmit"/></label></form>
    </body>
    </html>
    here is upload.php:

    PHP Code:
    $filename $_FILES['file']['tmp_file'];

    $file file($filename);

    $output fopen("output.txt","w+");

    foreach(
    $file as $line){
    fwrite($output,$line."\n");
    }

    fclose($file); 

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Your upload.php is missing some code to handle the uploaded file and to move it from the temporary area on the server to your desired location for it.

    There is a good tutorial on coding scripts to upload files at the w3schools website at:

    http://www.w3schools.com/PHP/php_file_upload.asp

  3. #3
    Join Date
    Mar 2010
    Posts
    28
    tirna,

    is there no way to work purely with the temporary file? I don't want to save the file that is uploaded, I just want to use it for the script and delete it.

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    I'm not sure because I've never tried it. I have always moved the temp file to a folder on the website and then did whatever with it.

    I would have thought, as a last resort if you can't use the temp file, you could move the temp file to an area on your website, do what you need to with it and then use unlink() to delete it from your web site.

  5. #5
    Join Date
    Mar 2010
    Posts
    28
    tirna,

    I have altered my code to what is below. Now, however, my code produces the following output. Also, after I click submit, the page brings up an open file box that I cannot explain. Finally, no file is created on the server in /tmp. Could there be something wrong in the php.ini file?

    PHP Code:
    <?php

    move_uploaded_file
    ($_FILES['file']['tmp_name'],"/tmp/".$_FILES['file']['name']);

    $file file("/tmp/".$_FILES['file']['name']);

    $output fopen("output.txt","w+");

    foreach(
    $file as $line){
        
    fwrite($output,$line."\n");
    }

    ?>

    Error message:

    Notice: Undefined index: file in F:\wamp\www\file2.php on line 3

    Notice: Undefined index: file in F:\wamp\www\file2.php on line 3

    Notice: Undefined index: file in F:\wamp\www\file2.php on line 5

    Warning: file(/tmp/) [function.file]: failed to open stream: No such file or directory in F:\wamp\www\file2.php on line 5

    Warning: Invalid argument supplied for foreach() in F:\wamp\www\file2.php on line 7

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    it's difficult for me to work out what the problem is because I don't think I see all your code.

    Maybe this template upload script can help you debug your script.

    PHP Code:
    <?php  
     
    //check if any errors occurred during the upload.
        
        
    if($_FILES['txtUploadFile']['error'] > 0)
        {
         echo 
    '<p>Problem: <br /></p>';
            switch (
    $_FILES['txtUploadFile']['error'])
            {
             case 
    1: echo '<p>File exceeded upload_max_filesize.<br />'; break;
                case 
    2: echo '<p>File exceeded max_file_size.<br />'; break;
                case 
    3: echo '<p>File only partially uploaded.<br />'; break;
                case 
    4: echo '<p>No file uploaded.<br />'; break;
            }
            
            exit;
        }
        
        
    //check if the file has the right MIME type
        
        
    if($_FILES['txtUploadFile']['type'] != 'text/plain')
        {
         echo 
    '<p>Problem: file is not plain text.<br /></p>';
            exit;
        }
        
    //move the file to desired folder
        
        
    $uploadedFile './uploads/'.$_FILES['txtUploadFile']['name']; //desired location and name of uploaded file
           
        
    if(@is_uploaded_file($_FILES['txtUploadFile']['tmp_name']))  //initial temp location of uploaded file
        
    {
         if(@!
    move_uploaded_file($_FILES['txtUploadFile']['tmp_name'], $uploadedFile))
            {
             echo 
    '<p>Problem: Could not move file to destination folder.<br /></p>';
                exit;
            }
        }
        else
        {
         echo 
    '<p>Problem: Possible file upload attack.<br />'.
             
    'File name: '.$_FILES['txtUploadFile']['name'];
            exit;
        }
        
        
    //at this point, the file was uploaded successfully
        
        
    echo '<p>Filename: '.$_FILES['txtUploadFile']['name'].' was uploaded successfully..!!';
        
    ?>

  7. #7
    Join Date
    Mar 2010
    Posts
    28
    the entire code for my script and html page is below. This is, as you can imagine, simply a dummy for a bigger script, but I am trying to get the upload to work before moving on.

    thing.html
    HTML Code:
    <html>
    <body>
    <form action="file2.php" action="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1073741824">
    <label><input type="file" name="file" id="file"/> 
    <input type="submit" name="fileSubmit" id="fileSubmit"/></label></form>
    </body>
    </html>
    file2.php
    PHP Code:
    <?php

    move_uploaded_file
    ($_FILES['file']['tmp_name'],"/tmp/".$_FILES['file']['name']);

    $file file("/tmp/".$_FILES['file']['name']);

    $output fopen("output.txt","w+");

    foreach(
    $file as $line){
        
    fwrite($output,$line."\n");
    }

    ?>

  8. #8
    Join Date
    Mar 2010
    Posts
    28
    using the script you gave me, I get the same error. I tried it through a different server, and i do not get the

    Notice: Undefined index: file in F:\wamp\www\file2.php on line 3

    type or error, but even if i upload a .txt it tells me the file is not plain text. also, the strange extra open file box still comes up.

  9. #9
    Join Date
    Mar 2010
    Posts
    2,803
    Comment out the mime-type check for now or change the mime type to the correct value for the file types you will be uploading.

    also, the strange extra open file box still comes up.
    What open file box??? I didn't see that mentioned in your previous posts.

    The template should work if your server is configured to allow file uploads. Unfortunately system admin is not my strong suit so I can't help much there if it isn't.

    If you haven't got a debugger, maybe insert echo statements at various points in your script to check the variable values and to make sure your script gets to the appropriate points in the script for it to run corectly.

  10. #10
    Join Date
    Mar 2010
    Posts
    28
    Quote Originally Posted by Marchingknight1 View Post
    tirna,

    I have altered my code to what is below. Now, however, my code produces the following output. Also, after I click submit, the page brings up an open file box that I cannot explain. Finally, no file is created on the server in /tmp. Could there be something wrong in the php.ini file?
    ...
    I can't figure out why this is happening, but after I click the submit button, after i have already uploaded the file (or attempted to), the script runs, displays the error messages or whatever, and then opens a new open file box.

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
  •  
HTML5 Development Center



Recent Articles