Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267582-choose-either-upload-from-url-or-file/
Share on other sites

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 ();
}

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.

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. ;)

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



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.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.