Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload Form Help

Status
Not open for further replies.

corexdev

Programmer
Joined
May 11, 2015
Messages
7
Location
US
i have the Following form and php code i have three Folders STORAGE1 STORAGE2 STORAGE3 in my Script directory, when i try to upload a File it Constantly puts it into the Root Folder Ignoring everything, below is what i have so far , anyone could help me see the error please let me know if i select STORAGE1 the File Should be uploaded to the STORAGE1 Folder etc "needing path in mysql to come soon"

#---------START-PHP FORM-----("uploadform.php")---------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<FORM ACTION="nextpage.php" METHOD="POST" ENCTYPE="multipart/form-data">
<P>Please browse and double-click the file to upload. Your filename must have one of the following extensions: .doc, .pdf, .ppt, .pps, .xls, .csv, .rtf, .txt, .htm, .html, .jpg, .gif, .png</p>
<P>File name: <br><INPUT class="inputform" TYPE="file" name="userfile" style="width:250px;"></p>
<P>Location:
<select name="location" onchange="">
<option name="STORAGE1" value="STORAGE1/">STORAGE1</option>
<option name="STORAGE2" value="STORAGE2/">STORAGE2</option>
<option name="STORAGE3" value="STORAGE3/">STORAGE3</option>
</select>
</p>
<P><INPUT class="inputform" TYPE="submit" VALUE="Upload File"></p>

<INPUT class="inputform" TYPE=hidden name="MAX_FILE_SIZE" value="513024">
<INPUT class="inputform" TYPE=hidden name="users_ID" value="<?print($users_ID);?>">
</FORM>
</body>
</html>


#---------START-PHP UPLOAD-----("nexpage.php")---------------------------

<?php

//set non-executable file extensions you'll accept as an upload
$okext = array(".doc", ".pdf", ".ppt", ".pps", ".xls", ".csv", ".rtf", ".txt", ".htm", ".html", ".jpg", ".gif", ".png");

//check for oversize files or empty uploads
$thesize = $_FILES['userfile']['size'];

if ( ($thesize > 3513024) || ($thesize == 0) ){
print('
<html>
<head>
<TITLE>File Upload 2</TITLE>
</head>
<body style="background:#CCCCFF">
<p>&nbsp;</p>
<p align=center>Error... No file, or file too large.<br>
If your file is too large, please post it somewhere on your company\'s website, then use a URL link to it instead.</p>
<p>&nbsp;</p>
<p align=center><a href="javascript:window.close();">Close Window</a></p>
</body>
</html>
');
exit;
}


//read the file from the temp location
$filename=$_FILES['userfile']['tmp_name'];
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize($filename));
fclose($fd);


//set the path for your saving directory
// $respath = "PLC/"; //include trailing slash
$loc = $_POST["$location"];
//$respath = $loc; //include trailing slash
$respath = $loc;
echo '$loc';

//check for valid file extension
$resext = "";
$extarray = explode(".", $_FILES['userfile']['name']);

if(count($extarray)>1)
{
$extpos = count($extarray)-1;
$resext = ".".$extarray[$extpos];
}

if (!(in_array($resext, $okext))){
print('
<html>
<head>
<LINK href="dladmin.css" type=text/css rel=stylesheet>
<TITLE>File Upload 2</TITLE>

</head>
<body style="background:#FFFFFF">
<p>&nbsp;</p>
<p align=center>Error... Invalid filename extension.<br>
Your filename must have one of the following extensions: .doc, .pdf, .ppt, .pps, .xls, .csv, .rtf, .txt, .htm, .html, .jpg, .gif, .png</p>
<p>&nbsp;</p>
<p align=center><a href="javascript:window.close();">Close Window</a></p>
</body>
</html>
');
exit;
}

//you can rename the file here if needed
$resname = $respath.$_FILES['userfile']['name'];

$resw = fopen($resname, "w");
fwrite($resw, $contents);
fclose($resw);


////// Don't forget to insert $resname into the db too! /////

?>
 
1. Have you checked what is actually getting set to your $loc variable? since that seems to be the basis for your $respath, are you sure that it is a valid path?

2. PHP has a function to move uploaded file to a specific directory.
Code:
[b][COLOR=#204A87]move_uploaded_file[/color][/b] ( [COLOR=#4E9A06]string[/color] [COLOR=#A40000]$filename[/color] , [COLOR=#4E9A06]string[/color] [COLOR=#A40000]$destination[/color] )

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top