GsXtAsY Posted August 19, 2008 Share Posted August 19, 2008 I have 3 total files so far and what i am wanting to do is instead of uploading the files into the main directory where the .php files are stored how would i go about having the files upload into a "upload" folder? Also I want to be able to display the files uploaded on one page without looking like a directory listing. Here is the code: <form name="form1" method="post" action="uploadForm2.php"> <p>Enter the amount of boxes you will need below. Max = 9.</p> <p> <input name="uploadNeed" type="text" id="uploadNeed" maxlength="1"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> <form name="form1" enctype="multipart/form-data" method="post" action="processFiles.php"> <p> <? // start of dynamic form $uploadNeed = $_POST['uploadNeed']; for($x=0;$x<$uploadNeed;$x++){ ?> <input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>"> </p> <? // end of for loop } ?> <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>"> <input type="submit" name="Submit" value="Submit"> </p> </form> <? $uploadNeed = $_POST['uploadNeed']; // start for loop for($x=0;$x<$uploadNeed;$x++){ $file_name = $_FILES['uploadFile'. $x]['name']; // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); $copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$file_name); // check if successfully copied if($copy){ echo "$file_name | uploaded sucessfully!<br>"; }else{ echo "$file_name | could not be uploaded!<br>"; } } // end of loop ?> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 19, 2008 Share Posted August 19, 2008 here is an upload script I have written: $sql = mysql_query("SELECT * FROM $TBL-NAME") or die(mysql_error()); $results = mysql_fetch_array($sql); $upload_dir = "PATH-TO-UPLOAD-DIR"; $upload_name = $_FILES['uploadedfile']['name']; $target_path = "uploads/"; $target_path = $target_path . basename($_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $sql = mysql_query("INSERT INTO table_name (column1, column2, column3) VALUES ('$column1', '$column2', '$column3')") or die(mysql_error()); echo '<h2>Upload successful!</h2>'; echo '<p>Thanks for your contribution. Here is the information on <strong>' . $_FILES['uploadedfile']['name'] . '</strong>!</p>'; echo '<p>Name of Ad: ' . $_FILES['uploadedfile']['name'] . '</p>'; echo '<p>File width and height: ' . $ad_size . '</p>'; echo '<p>Company: ' . $company . '</p>'; if($comments) { echo '<p>Comments: ' . $comments . '</p>'; } echo '<h2><a href="upload.php?action=upload">Upload another file?</a></h2>'; } else { echo '<h2>Upload failed!</h2>'; echo $_FILES['uploadedfile']['error']; } Quote Link to comment Share on other sites More sharing options...
GsXtAsY Posted August 19, 2008 Author Share Posted August 19, 2008 Thank you I will try that...now for displaying the uploads on the page allowing users to download the files...how would i go about doing that? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 19, 2008 Share Posted August 19, 2008 You would assemble your download link like this: <a href="uploads/download.php?id=' . $id . '"><img src="uploads/image.jpg" border="0" /></a> here is a script I use for my downloads, now the download.php file must be in the uploads directory <?php $base_dir = "PATH_TO_UPLOAD_DIR""; //The place you'll store the files $id = $_GET['id']; include "../includes/sql.php"; $query = mysql_query("SELECT column_name from table_name where upload_id = '$id'") or die("ERROR: " . mysql_error()); $row = mysql_fetch_array($query); $file = $row['upload_name']; $file_extension = strtolower(substr(strrchr($file,"."),1)); $filename = $base_dir . $file; if ( ! file_exists( $file ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; } switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile($file); exit(); ?> Quote Link to comment Share on other sites More sharing options...
GsXtAsY Posted August 19, 2008 Author Share Posted August 19, 2008 I cant get this upload script to work...any extra help would be awesome! Quote Link to comment Share on other sites More sharing options...
GsXtAsY Posted August 19, 2008 Author Share Posted August 19, 2008 Can anyone tell me what i need to change in my script so I can have these files save to an Upload folder on my first post script Thanks Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 19, 2008 Share Posted August 19, 2008 make sure you have enctype="multipart/form-data" in the form tag Quote Link to comment Share on other sites More sharing options...
GsXtAsY Posted August 19, 2008 Author Share Posted August 19, 2008 ok going with a new script...but im getting an error... Warning: opendir(http://www.domain.com/acceptance/admin/upload/files/) [function.opendir]: failed to open dir: not implemented in /home/nlvip/public_html/acceptance/admin/upload/multiple_upload_example.php on line 105 Line 105 is: if ($handle = opendir($dir)) { Code: <?php $folder = $DOCUMENT_ROOT."/acceptance/admin/upload/files/"; // the folder which you want to list the files from function select_files($dir) { global $PHP_SELF; $teller = 0; if ($handle = opendir($dir)) { $mydir = "<p><strong>Choose image to download from the list below...</strong></p>\n"; $mydir .= "<form name=\"form1\" method=\"post\" action=\"thisDownloadsPage.php\">\n"; // change page to your download page... $mydir .= " <select name=\"file_in_folder\" id=\"file_in_folder\">\n"; $mydir .= " <option value=\"\" selected>... </option>\n"; while (false !== ($file = readdir($handle))) { $files[] = $file; } sort($files); foreach ($files as $val) { if ($val != "." && $val != "..") { $mydir .= " <option value=\"".$val."\">"; $mydir .= (strlen($val) > 30) ? substr($val, 0, 30)."...</option>\n" : $val."</option>\n"; $teller++; } } $mydir .= " </select>"; $mydir .= "<p><input type=\"submit\" class=\"submit\" name=\"download\" value=\"Download\"></p>"; $mydir .= "</form>\n"; closedir($handle); } if ($teller == 0) { echo "<p> </p>\n<p> </p>\n<p class=\"green\">(There are currently no files available for downloading.)</span>"; // this will display if the folder is empty } else { echo $mydir; } } if (isset($download)) { $fullPath = $folder.$_POST['file_in_folder']; if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "jpg": header("Content-type: image/jpg"); header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use "attachment;" for forcing download rather than just viewing on page - as jpgs and gifs will be displayed if attachment isn't indicated. break; case "zip": header("Content-type: application/zip"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } header("Content-length: $fsize"); header("Cache-control: private"); while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; } ?> Quote Link to comment Share on other sites More sharing options...
GsXtAsY Posted August 19, 2008 Author Share Posted August 19, 2008 What im trying to do it display all the files uploaded to the server through the upload script. is there any easier way to doing this? I want them to be able to download these files too...you can see the code I have from the post above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.