www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to perform a file upload in php

  1. #1
    Join Date
    May 2013
    Posts
    2

    How to perform a file upload in php

    Hi, i am an asp.net developer but recently had to switch to php to design a website for a friend, i am currently trying out the file upload but keep on getting errors.
    error: Undefined index
    I've tried soo many scripts online but none work, so could you please here me out.

    Code:
    <form action="getfile.php" method="post" enctype="multipart/form-data>
    <input type="file" name="uploadedFile" >
    <input type="submit" value="Upload File">
    </form>
    Code:
    <?php
    echo $_FILES['uploadedFile']['name'];
    if( $_FILES['uploadedFile']['name'] != "" )
    {
       copy( $_FILES['uploadedFile']['name'], "uploads/" ) or 
               die( "Could not copy file!");
    }
    else
    {
        die("No file specified!");
    }
    ?>
    i tried this too:
    Code:
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }

  2. #2
    Join Date
    Dec 2012
    Posts
    97
    The second script should work, you seem to be missing a capital F on "uploadedFile" though.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    One thing you'll want in order to avoid "undefined index" type error messages is to make sure the $_FILES array element is there before you try to access it, e.g.:
    PHP Code:
    if( ! empty($_FILES['uploadedFile'])) {
        
    // do stuff with $_FILES['uploadedFile']
    }
    else {
        
    // handle error condition of no uploaded file, perhaps including some debug code for now?

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2009
    Location
    Texas
    Posts
    124
    Did you look at the sticky for this in this forum?
    Samantha Morrison
    Knock-Out Web Designs
    http://kowebdesigns.com
    Website Developer/Designer

Thread Information

Users Browsing this Thread

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

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