Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Upload file set name to var???

    Hi,

    I am trying ind to make a script that will upload a file and set the name to a variable. I have been looking at php upload scripts and either they don't work or i can't set the file name to a variable. Here is my html form:

    <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table align="center">
    <tr valign="baseline">
    <td nowrap align="right">Title:</td>
    <td><input type="text" name="title" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Game:</td>
    <td><select name="game">
    <?php
    do {
    ?>
    <option value="<?php echo $row_GamesRS['name']?>" ><?php echo $row_GamesRS['name']?></option>
    <?php
    } while ($row_GamesRS = mysql_fetch_assoc($GamesRS));
    ?>
    </select>
    </td>
    <tr>
    <tr valign="baseline">
    <td nowrap align="right">Type:</td>
    <td><select name="type">
    <option value="Picture" <?php if (!(strcmp("Picture", ""))) {echo "SELECTED";} ?>>Picture</option>
    <option value="Video" <?php if (!(strcmp("Video", ""))) {echo "SELECTED";} ?>>Video</option>
    </select>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Media:</td>
    <td><input name="location" type="file" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Thumbnail:</td>
    <td><input name="thumbnail" type="file" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td><input type="submit" value="Insert record"></td>
    </tr>
    </table>
    <input type="hidden" name="platform" value="360">
    <input type="hidden" name="MM_insert" value="form1">
    </form>


    There is a few other fields there but they are all working fine. I am yet to find some php code to upload that I can get to work, and I don't know how to set the file name to a variable.

    Thanks,
    ~ Christian

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the filename of an uploaded file is includes as part of the $_FILES superglobal, in your case it would be $_FILES['location']['name']
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Got this upload code:

    PHP Code:
    // Where the file is going to be placed 
    $target_path "../media/";

    /* Add the original filename to our target path.  
    Result is "uploads/filename.extension" */
    $target_path $target_path basename$_FILES['location']['name']); 
    $_FILES['location']['tmp_name'];  

    if(
    move_uploaded_file($_FILES['location']['tmp_name'], $target_path)) {
        echo 
    "The file ".  basename$_FILES['location']['name']). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file, please try again!";

    But i get the error:

    Code:
    Notice: Undefined index: location in /Applications/MAMP/htdocs/SPOTXBOX.COM - NEW SITE/admin/newmedia.php on line 87
    
    Notice: Undefined index: location in /Applications/MAMP/htdocs/SPOTXBOX.COM - NEW SITE/admin/newmedia.php on line 88
    
    Notice: Undefined index: location in /Applications/MAMP/htdocs/SPOTXBOX.COM - NEW SITE/admin/newmedia.php on line 90
    There was an error uploading the file, please try again!
    Notice: Undefined index: location in /Applications/MAMP/htdocs/SPOTXBOX.COM - NEW SITE/admin/newmedia.php on line 106
    
    Notice: Undefined index: thumbnail in /Applications/MAMP/htdocs/SPOTXBOX.COM - NEW SITE/admin/newmedia.php on line 108

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You don't have the encoding type set for the form. It must be multipart/form-data or the files will not come through.
    Deliver yesterday, code today, think tomorrow.

  • #5
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Fixed It!

    Thanks Hammer!


  •  

    Posting Permissions

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