Jump to content
glassfish

Keeping it Separately with Object Oriented Programming

Recommended Posts

I have this image upload script here:

<?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 
    
	$target_path = $_SERVER['DOCUMENT_ROOT'] . "/ubergallery/multiple_image_upload/uploads/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable
        
		$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       
      
	  if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>

This script also uses javascript for multiple uploading of images.

 

I am looking to add a query to this where I am looking to have the image file names stored in the database.

 

The original image file name should get stored as well, yet the file name itself should get a unique id.

 

I may have to build this query inside this upload script. I was wondering if I can keep that separately with Object Oriented Programming, if somebody just could show me "how it could look like", when it comes to having this done with Object Oriented Programming, I would appreciate it a lot.

 

Share this post


Link to post
Share on other sites

What exactly is it that you're struggling with? Do you want to know how to store information into a database?

 

You need to read up on either Mysqli or PDO to learn how to insert the data.

 

In terms of your script, you will make the database insert at the part where your validation returns no errors, ie the file type/size was accepted. You will then insert the variable which holds the file name into your database.

 

Or create a flag such as $errors = FALSE at the start of the script; and any time the validation fails, make the flag TRUE. Then at the end simple do the following;

if(!$errors){
// script to insert into database
}
Edited by paddy_fields

Share this post


Link to post
Share on other sites

 

What exactly is it that you're struggling with?

It has been Object Oriented Programming itself. I was wondering "how it would look like" when looking to keep the code organized and separately from each other when it comes having this done with Object Oriented Programming.

 

The above script is written in procedual style, I would "have to" build the query "into" the script. I could be looking to also write the script in Object Oriented Programming and then add the query also in Object Oriented Programming to it.

Edited by glassfish

Share this post


Link to post
Share on other sites

I fail to see why one would need to consider "script design" changes just because one switches from procedural to oo.  If one is already managing to keep business logic physically separated from the presentation logic, how does the change in tools affect this?

Share this post


Link to post
Share on other sites

how does the change in tools affect this?

It also is a "how to do it" question, I was looking for pointers.

Share this post


Link to post
Share on other sites

If you want something added but separate, why not just add a function call that passes the necessary args to the function?  Then design the function anyway you want.

Share this post


Link to post
Share on other sites

It could look like this:

 

public function uploadAction(Request $request) {
  $uploadService = $this->get('upload_service');
  $form = $uploadService->getForm();
  
  if ($uploadService->handle($form, $request)) {
      $request->getSession()->getFlashBag()->add('success', 'Image uploaded successfully!');
      return $this->redirect($this->generateUrl('foo_bar'));
  }
  
  return array();
}
Yes, this is the same script as what you wrote up there. And this is typical for OO programming. It hides ALL the details. The UploadService would do something like this:

 

public function handle(FormInterface $form, Request $request) {
  $form->handleRequest($request);
  
  if (!$form->isValid()) {
      return false;
  }
  
  foreach ($form->get('upload')->getData() as $uploadedFile) {
      $file = $uploadedFile->move($this->targetDirectory);
      
      $upload = new Upload($uploadedFile->getClientOriginalName(), $file->getMimeType(), $file->getSize());
      $this->em->persist($upload);
  }
  
  $this->em->flush();
  
  return true;
}
This is an example using Symfony framework. Edited by ignace

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.