smulder Posted August 28, 2013 Share Posted August 28, 2013 i have this code which will resize the uploaded image to its max level.all i want is to transfer my images into a folder if i will use this format move_uploaded_file($_FILES['image']['tmp_name'],'uploads/'. $_FILES['image']['name']); only one image will be place on the folder and its the original image. move_uploaded_file($_FILES['image']['tmp_name'],$_FILES['image']['name']); if i will used this code all resize images will be displayed but not in the folder which i want the image to be place upload_image.php Quote Link to comment https://forums.phpfreaks.com/topic/281649-moving-multiple-uploaded-images/ Share on other sites More sharing options...
jazzman1 Posted September 1, 2013 Share Posted September 1, 2013 I see a lot of members here asking for solutions "How to upload multiple images to the fileserver and store some information into a database"So, I decided to provide mine, hope that helps someone.1.First off, for uploading multiple images I'm using this API .You would download the free version created under GPLv2 licence.2.Second, you need to create a database. To keep the things simple, I've been created a database with name - "images" and a table with name "img_tbl" with id and name columns.3.Third, open up a file called - upload.php, find and add next lines of code: // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); // create your insert query statement $query = "INSERT INTO `images`.`img_tbl` VALUES(NULL,'$fileName')"; $result = mysql_query($query,$conn) or die(mysql_error()); $last_id = mysql_insert_id($conn); // if there is affected rows // proceed with next select statement if(mysql_affected_rows()) { // call select statement $query = "SELECT `name` FROM `images`.`img_tbl` WHERE id = ".$last_id; $result = mysql_query($query) or die(mysql_error()); // fetch a row by last inserted id $row = mysql_fetch_assoc($result); // call resize img photo resize_img($row['name']); } 4. Open up a new page and create your resize image function, save the file as resize_photo.php.This function will resized your original image to thumbnail. It's been created by the author of phpvideotutorials.com, few years ago.Check on the web and you will find much better solutions, I think. ** * The following four functions resizes the photos uploaded by a user * creates the thumbnail photo reproduction of the originally uploaded image * @param int $id * @return bool */ function resize_img($name) { // set original image location $img = './uploads/original/' . $name; // set our image canvas $canvas_width = 75; $canvas_height = 75; // create a blank canvas $canvas = imagecreatetruecolor($canvas_width, $canvas_height); // get width and height of original image list($img_width, $img_height) = getimagesize($img); $original = imagecreatefromjpeg($img); // copy original onto canvas imagecopyresampled($canvas, $original, 0, 0, (($img_width / 2 ) - ( ($canvas_width / 2) * 4 )), (($img_height / 2 ) - ( ($canvas_height / 2) * 4 )), $img_width / 4, $img_height / 4, $img_width, $img_height); if (imagejpeg($canvas, './uploads/thumbs/' . $name, 75)) { return true; } else { return false; } } 5. Create a new file where you will display your thumnails:display_thumbnails.php <?php include 'db_config.php'; $query = "SELECT `name` FROM `images`.`img_tbl` WHERE 1"; $result = mysql_query($query) or die(mysql_error()); $outputs = array(); while ($row = mysql_fetch_assoc($result)) { $outputs[] = $row; } foreach ($outputs as $output) { echo "<a href=./uploads/original/{$output['name']} style=margin-right:5px><img src=./uploads/thumbs/{$output['name']} /></a>"; } 6. Create a db_config.php file with your database credentials $conn = mysql_connect('db_host', 'db_name','db_pass') or die(mysql_error()); $db_name = mysql_select_db('images', $conn) or die(mysql_error()); 7. Inside the upload directory, create two new directories with names: original and thumbs8. Include "db_config.php" and "resize_photo.php" to upload.php.Upload.php file should be similar like that: <?php include 'db_config.php'; include 'resize_photo.php'; /** * upload.php * * Copyright 2009, Moxiecode Systems AB * Released under GPL License. * * License: http://www.plupload.com/license * Contributing: http://www.plupload.com/contributing */ // HTTP headers for no cache etc header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // Settings $targetDir = './uploads/original'; $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // 5 minutes execution time set_time_limit(5 * 60); // Uncomment this one to fake upload time // usleep(5000); // Get parameters $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; // Clean the fileName for security reasons $fileName = preg_replace('/[^\w\._]+/', '_', $fileName); // Make sure the fileName is unique but only if chunking is disabled if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) { $ext = strrpos($fileName, '.'); $fileName_a = substr($fileName, 0, $ext); $fileName_b = substr($fileName, $ext); $count = 1; while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) $count++; $fileName = $fileName_a . '_' . $count . $fileName_b; } $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; // Create target dir if (!file_exists($targetDir)) mkdir($targetDir); // Remove old temp files if ($cleanupTargetDir) { if (is_dir($targetDir) && ($dir = opendir($targetDir))) { while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; // Remove temp file if it is older than the max age and is not the current file if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge) && ($tmpfilePath != "{$filePath}.part")) { unlink($tmpfilePath); } } closedir($dir); } else { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); } } // Look for the content type header if (isset($_SERVER["HTTP_CONTENT_TYPE"])) $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; if (isset($_SERVER["CONTENT_TYPE"])) $contentType = $_SERVER["CONTENT_TYPE"]; // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5 if (strpos($contentType, "multipart") !== false) { if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen($_FILES['file']['tmp_name'], "rb"); if ($in) { while ($buff = fread($in, 4096)) fwrite($out, $buff); } else die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); fclose($in); fclose($out); unlink($_FILES['file']['tmp_name']); } else die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } else die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); } else { // Open temp file $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen("php://input", "rb"); if ($in) { while ($buff = fread($in, 4096)) fwrite($out, $buff); } else die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); fclose($in); fclose($out); } else die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); // create your insert query statement $query = "INSERT INTO `images`.`img_tbl` VALUES(NULL,'$fileName')"; $result = mysql_query($query,$conn) or die(mysql_error()); $last_id = mysql_insert_id($conn); // if there is affected rows // proceed with next select statement if(mysql_affected_rows()) { // call select statement $query = "SELECT `name` FROM `images`.`img_tbl` WHERE id = ".$last_id; $result = mysql_query($query) or die(mysql_error()); // fetch a row by last inserted id $row = mysql_fetch_assoc($result); // call resize img photo resize_img($row['name']); } } In this example, I'm using the mysql library just for the test. You should avoid it, just create the same app using mysqli or PDO!You can also validate every sql insert value and make your app much more security, but just not a purpose here.Here it is! Quote Link to comment https://forums.phpfreaks.com/topic/281649-moving-multiple-uploaded-images/#findComment-1447615 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.