IronWarrior Posted July 31, 2008 Share Posted July 31, 2008 We have 2 files, the first being the file containing the HTML form: <h3> List an item </h3> <p> This is the section of the website where you can list a new item for sale. </p> <form action="index.php?page=submitAnItem" enctype="multipart/form-data" method="post"> Vehicle Name: <input type="text" name="vehicleName" size="35"> <br /> Vehicle Description: <textarea name="vehicleDescription" cols="60" rows="30"></textarea> <br /> Vehicle Price <input type="text" name="vehiclePrice"> <br /> Vehicle Type <input type="text" name="vehicleType"> <br /> Vehicle Category <br /> <input type="radio" name="vehicleCategory" value="1"> Armoured Vehicle <br /> <input type="radio" name="vehicleCategory" value="2"> Truck <br /> <input type="radio" name="vehicleCategory" value="3"> Jeep <br /> Upload Images: <br /> <input type="hidden" name="MAX_FILE_SIZE" value="3000000"> <input type="file" name="file" id="file" /> <br /> <input type="file" name="itemImageTwo"> <br /> <input type="file" name="itemImageThree"> <br /> <input type="file" name="itemImageFour"> <br /> <input type="hidden" name="validTransaction" value="true"> <input type="submit" value="Submit Listing"> </form> The second being the file containing the php: <?php if (isset($_POST["validTransaction"])) { $vehicleName = $_POST["vehicleName"]; $vehicleDescription = $_POST["vehicleDescription"]; $vehiclePrice = $_POST["vehiclePrice"]; $vehicleType = $_POST["vehicleType"]; $vehicleCategory = $_POST["vehicleCategory"]; $trimmedVehicleName = str_replace(" ","",$vehicleName); $folderLocation = "images/".$trimmedVehicleName; $currentTime = time(); $newName = $folderLocation."/".$currentTime.$trimmedVehicleName.".jpg"; //Checking if all fields are present if ($vehicleName == "" || $vehicleDescription == "" || $vehiclePrice == "" || $vehicleType == "" || $vehicleCategory == "") { echo "Not all infomation has been completed, please use the return button to re-submit the infomation"; $error = true; } else { //Starts Here if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 3000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("$folderLocation/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { mkdir("$folderLocation"); move_uploaded_file($_FILES["file"]["tmp_name"], "$folderLocation/" . $_FILES["file"]["name"]); $oldName = $folderLocation."/".$_FILES["file"]["name"]; rename ($oldName, $newName); // Start Image Resize // This is the temporary file created by PHP $uploadedfile = $newName; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); // For our purposes, I have resized the image to be // 600 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 600, simply change the $newwidth variable $newwidth=300; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = $folderLocation."/". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request unlink($newName); rename ($oldName, $newName); //Finish Image Resize echo "Stored in: " . "$folderLocation/"; } } } else { echo "Invalid file"; echo ($_FILES["file"]["error"]); } //Ends Here mysql_select_db($databaseName); $sql="INSERT INTO vehiclesforsale (vehicleName, vehicleDescription, vehiclePrice, vehicleType, vehicleCatagory) VALUES ('$vehicleName','$vehicleDescription','$vehiclePrice','$vehicleType','$vehicleCategory')"; if (!mysql_query($sql)) { echo $sql; die('Error: ' . mysql_error()); $error = true; } if (!$error) { $_POST["validTransaction"] = false; ?> <script type="text/javascript"> <!-- alert("Record Added <?echo $folderLocation;?>") window.location = "index.php?page=listAnItem" //--> </script> <? } } } else { echo "Invalid Transaction"; } ?> Now this all works, but it needs a new loop adding to upload all four of the images.... Can anyone assist? Link to comment https://forums.phpfreaks.com/topic/117556-image-uploading/ Share on other sites More sharing options...
ikmyer Posted July 31, 2008 Share Posted July 31, 2008 http://us3.php.net/features.file-upload check out example #3 Link to comment https://forums.phpfreaks.com/topic/117556-image-uploading/#findComment-604653 Share on other sites More sharing options...
ikmyer Posted July 31, 2008 Share Posted July 31, 2008 or... http://www.phpeasystep.com/workshopview.php?id=2 Link to comment https://forums.phpfreaks.com/topic/117556-image-uploading/#findComment-604655 Share on other sites More sharing options...
IronWarrior Posted July 31, 2008 Author Share Posted July 31, 2008 Still not got it, tried to use example 3, but couldnt get it to work, codes as follows: <?php if (isset($_POST["validTransaction"])) { $vehicleName = $_POST["vehicleName"]; $vehicleDescription = $_POST["vehicleDescription"]; $vehiclePrice = $_POST["vehiclePrice"]; $vehicleType = $_POST["vehicleType"]; $vehicleCategory = $_POST["vehicleCategory"]; $trimmedVehicleName = str_replace(" ","",$vehicleName); $folderLocation = "images/".$trimmedVehicleName; $currentTime = time(); $newName = $folderLocation."/".$currentTime.$trimmedVehicleName.".jpg"; //Checking if all fields are present if ($vehicleName == "" || $vehicleDescription == "" || $vehiclePrice == "" || $vehicleType == "" || $vehicleCategory == "") { echo "Not all infomation has been completed, please use the return button to re-submit the infomation"; $error = true; } else { foreach($_FILES["file"]["error"] as $key => $error) { //Starts Here if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 3000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("$folderLocation/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { mkdir("$folderLocation"); move_uploaded_file($_FILES["file"]["tmp_name"], "$folderLocation/" . $_FILES["file"]["name"]); $oldName = $folderLocation."/".$_FILES["file"]["name"]; rename ($oldName, $newName); // Start Image Resize // This is the temporary file created by PHP $uploadedfile = $newName; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); // For our purposes, I have resized the image to be // 600 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 600, simply change the $newwidth variable $newwidth=300; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = $folderLocation."/". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request unlink($newName); rename ($oldName, $newName); //Finish Image Resize echo "Stored in: " . "$folderLocation/"; } } } else { echo "Invalid file"; echo ($_FILES["file"]["error"]); } } //Ends Here mysql_select_db($databaseName); $sql="INSERT INTO vehiclesforsale (vehicleName, vehicleDescription, vehiclePrice, vehicleType, vehicleCatagory) VALUES ('$vehicleName','$vehicleDescription','$vehiclePrice','$vehicleType','$vehicleCategory')"; if (!mysql_query($sql)) { echo $sql; die('Error: ' . mysql_error()); $error = true; } if (!$error) { $_POST["validTransaction"] = false; ?> <script type="text/javascript"> <!-- alert("Record Added <?echo $folderLocation;?>") window.location = "index.php?page=listAnItem" //--> </script> <? } } } else { echo "Invalid Transaction"; } ?> Changes on line 22, tried using the for each. Have also changed the form as follows: <h3> List an item </h3> <p> This is the section of the website where you can list a new item for sale. </p> <form action="index.php?page=submitAnItem" enctype="multipart/form-data" method="post"> Vehicle Name: <input type="text" name="vehicleName" size="35"> <br /> Vehicle Description: <textarea name="vehicleDescription" cols="60" rows="30"></textarea> <br /> Vehicle Price <input type="text" name="vehiclePrice"> <br /> Vehicle Type <input type="text" name="vehicleType"> <br /> Vehicle Category <br /> <input type="radio" name="vehicleCategory" value="1"> Armoured Vehicle <br /> <input type="radio" name="vehicleCategory" value="2"> Truck <br /> <input type="radio" name="vehicleCategory" value="3"> Jeep <br /> Upload Images: <br /> <input type="hidden" name="MAX_FILE_SIZE" value="3000000"> <input type="file" name="file[]"> <br /> <input type="file" name="file[]"> <br /> <input type="file" name="file[]"> <br /> <input type="file" name="file[]"> <br /> <input type="hidden" name="validTransaction" value="true"> <input type="submit" value="Submit Listing"> </form> Link to comment https://forums.phpfreaks.com/topic/117556-image-uploading/#findComment-604726 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.