glassfish Posted October 10, 2014 Share Posted October 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/ Share on other sites More sharing options...
paddy_fields Posted October 10, 2014 Share Posted October 10, 2014 (edited) 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 October 10, 2014 by paddy_fields Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493224 Share on other sites More sharing options...
glassfish Posted October 10, 2014 Author Share Posted October 10, 2014 (edited) 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 October 10, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493226 Share on other sites More sharing options...
ginerjm Posted October 10, 2014 Share Posted October 10, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493227 Share on other sites More sharing options...
glassfish Posted October 10, 2014 Author Share Posted October 10, 2014 how does the change in tools affect this? It also is a "how to do it" question, I was looking for pointers. Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493228 Share on other sites More sharing options...
ginerjm Posted October 10, 2014 Share Posted October 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493232 Share on other sites More sharing options...
ignace Posted October 11, 2014 Share Posted October 11, 2014 (edited) 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 October 11, 2014 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/291552-keeping-it-separately-with-object-oriented-programming/#findComment-1493312 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.