...

View Full Version : Upload file set name to var???



the-dream
01-02-2008, 08:04 PM
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

GJay
01-02-2008, 08:37 PM
the filename of an uploaded file is includes as part of the $_FILES superglobal, in your case it would be $_FILES['location']['name']

the-dream
01-02-2008, 08:50 PM
Got this upload 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:


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

hammer65
01-02-2008, 08:57 PM
You don't have the encoding type set for the form. It must be multipart/form-data or the files will not come through.

the-dream
01-02-2008, 09:38 PM
Fixed It!

Thanks Hammer!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum