bbmak Posted August 26, 2012 Share Posted August 26, 2012 I try to make my script allow either upload from url or from file. I want to know how do I make this condition much cleaner. :'( I do not want to retype all the insert code and upload image code again. Are there anyway to make my condition easier. $item_image = $_FILES['item_image']['tmp_name']; if (!isset($item_image)) $item = $POST_['item_url']; //if no file is selected for upload, use url upload // all the mysql insert code down here. <<--- same as the bottom insert code. else { $item_image = addslashes(file_get_contents($_FILES['item_image']['tmp_name'])); $item_image_name = addslashes($_FILES['item_image']['name']); $item_image_size = getimagesize($_FILES['item_image']['tmp_name']); //check if image or not using $image_size if ($item_image_size==FALSE) echo "This is not an image file."; //check if image or not using $image_size else { // all the mysql insert code down here. <<--- same as the above insert code. } } Thank you for reading. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/ Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 The simplest way to do this, is to check if the $_FILES array is populated, and if not then if the user has submitted an URL. Then deal with the specifics of each methods, before going back to the common functionality. A quick example in pseudo-code: if (form_posted ()) { // Do generic validation. if (file_uploaded ()) { $file = handle_uploaded_file (); } elseif (url_posted ()) { $file = download_file ($url); } db->save_details ($file); } else { show_form (); } Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372487 Share on other sites More sharing options...
bbmak Posted August 26, 2012 Author Share Posted August 26, 2012 I don't get this part. db->save_details ($file); ?? that where you insert data into your database? I thought this part should inside the else statement. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372629 Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 Yes it is, and no it shouldn't. Though, what you should do is to actually verify that the file was handled correctly, between the IF-test that determines the correct action to take, and the actual insertion into the database. In my example I've simply assumed that the file handling, whether it be an upload or URL download, always succeeds. This will not be true in a proper system, but wasn't essential to the example. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372631 Share on other sites More sharing options...
bbmak Posted August 26, 2012 Author Share Posted August 26, 2012 :'( Too many if else is driving me crazy. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372665 Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 Don't try to do everything at once, but focus on one step at a time. Try to de-construct the problem into its smallest constituents, and then figure out what you need to solve them. Once you've found a solution for one step, look forward to the next one, and see if the result from the previously solved ones gives you what you need for the next. If not, then go back and redesign the solution until you get what you need. The trick is to not only see the whole forest, but being able to separate and keep track of each individual tree as well. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372671 Share on other sites More sharing options...
bbmak Posted August 26, 2012 Author Share Posted August 26, 2012 This is my code, but somehow it is not working. When I insert the image, the url's file_get_contents() will give me an error. On the other hand, when I insert an url, the file upload's file_get_contents() will give me an error. I hope somebody can help me a littlebit, I have actually no idea what is wrong. //errors checking if (empty($item_name) || empty($item_link) || empty($item_price) && '0' != $item_price) { $errors[] = 'Item Name, Item Link, and Item Price Cannot Be Empty.'; } else { $validate = new validate(); if(!$validate->validateURL($item_link)) { $errors[] = 'This is not a URL.'; } if (!is_numeric($item_price)) { $errors[] = 'Item Price Must be a number.'; } } if (!empty($errors)) { foreach ($errors as $error) { echo '<strong>', $error, '</strong><br />'; } } else { //errors checking $item_url = $_POST['item_url']; if(isset($item_url)) { $item_image_name = explode("/", $item_url); //spliting file name $item_image_name = end($item_image_name); $imgData = file_get_contents($item_url); $myFile = "../tmp_download/" .$item_image_name; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $imgData); fclose($fh); $item_image_size = getimagesize($myFile); //rename file name $splitName = explode(".", $item_image_name); //spliting file name $fileExt = end($splitName); //getting file extension //rename file name $newFileName = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', str_replace(" ", "", str_replace('"', "", stripslashes(strtolower($item_image_name.'.'.$fileExt))))); //check if image or not using $image_size if ($item_image_size==FALSE) echo "This is not an image file."; //check if image or not using $image_size else { $target = "$home_root/public_html/item_image/$newFileName"; if (copy($myFile,$target)) { unlink($myFile); } } } //URL UPLOAD CHECK $item_image = $_FILES['item_image']['tmp_name']; //Image Upload Check if (isset($item_image)) { $item_image = addslashes(file_get_contents($_FILES['item_image']['tmp_name'])); $item_image_name = addslashes($_FILES['item_image']['name']); $item_image_size = getimagesize($_FILES['item_image']['tmp_name']); //rename file name $splitName = explode(".", $item_image_name); //spliting file name $fileExt = end($splitName); //getting file extension //rename file name $newFileName = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', str_replace(" ", "", str_replace('"', "", stripslashes(strtolower($item_image_name.'.'.$fileExt))))); //check if image or not using $image_size if ($item_image_size==FALSE) echo "This is not an image file."; //check if image or not using $image_size else { $target = "$home_root/public_html/item_image/$newFileName"; if(move_uploaded_file($_FILES['item_image']['tmp_name'], $target)) { echo "image is uploaded <br />"; } else { echo "image uploaded fail <br />"; } } } //Image Upload Check Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372690 Share on other sites More sharing options...
bbmak Posted August 26, 2012 Author Share Posted August 26, 2012 It is driving me crazy. When I use if (!empty($item_url)) and if (!empty($item_image)) are working, the script is good. However, when I just use if (isset($item_url)) and if (isset($item_image)) are not working. when I use isset, it keeps saying the other one is empty, and cannot get_file_contents(). For example, if I upload with file, it will say the url's get_file_contents() cannot be empty. when I upload with url, it will say the file's get_file_contents() cannot be empty. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372702 Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 Please properly indent your code. It makes it a lot easier for everyone involved, you including, to read the code and thus spot the issues. Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372703 Share on other sites More sharing options...
bbmak Posted August 26, 2012 Author Share Posted August 26, 2012 Sorry for the messy. Basically it is 3 parts of my code. Now !empty is working. Can anybody tell me why isset is not working in my case? Top validation, name, link, and price. //errors checking if (empty($item_name) || empty($item_link) || empty($item_price) && '0' != $item_price) { $errors[] = 'Item Name, Item Link, and Item Price Cannot Be Empty.'; } else { $validate = new validate(); if(!$validate->validateURL($item_link)) { $errors[] = 'This is not a URL.'; } if (!is_numeric($item_price)) { $errors[] = 'Item Price Must be a number.'; } } if (!empty($errors)) { foreach ($errors as $error) { echo '<strong>', $error, '</strong><br />'; } } else { URL Upload: //URL UPLOAD CHECK $item_url = $_POST['item_url']; if (!empty($item_url)) { $item_image_name = explode("/", $item_url); //spliting file name $item_image_name = end($item_image_name); $imgData = file_get_contents($item_url); $myFile = "../tmp_download/" .$item_image_name; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $imgData); fclose($fh); $item_image_size = getimagesize($myFile); //rename file name $splitName = explode(".", $item_image_name); //spliting file name $fileExt = end($splitName); //getting file extension //rename file name $newFileName = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', str_replace(" ", "", str_replace('"', "", stripslashes(strtolower($item_image_name.'.'.$fileExt))))); //check if image or not using $image_size if ($item_image_size==FALSE) echo "This is not an image file."; //check if image or not using $image_size else { $target = "$home_root/public_html/item_image/$newFileName"; if (copy($myFile,$target)) { unlink($myFile); } } } //URL UPLOAD CHECK File Upload Check: //Image Upload Check $item_image = $_FILES['item_image']['tmp_name']; if (!empty($item_image)) { $item_image = addslashes(file_get_contents($_FILES['item_image']['tmp_name'])); $item_image_name = addslashes($_FILES['item_image']['name']); $item_image_size = getimagesize($_FILES['item_image']['tmp_name']); //rename file name $splitName = explode(".", $item_image_name); //spliting file name $fileExt = end($splitName); //getting file extension //rename file name $newFileName = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', str_replace(" ", "", str_replace('"', "", stripslashes(strtolower($item_image_name.'.'.$fileExt))))); //check if image or not using $image_size if ($item_image_size==FALSE) echo "This is not an image file."; //check if image or not using $image_size else { $target = "$home_root/public_html/item_image/$newFileName"; if(move_uploaded_file($_FILES['item_image']['tmp_name'], $target)) { echo "image is uploaded <br />"; } else { echo "image uploaded fail <br />"; } } } //Image Upload Check Quote Link to comment https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/#findComment-1372707 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.