简体   繁体   中英

PHP upload 4 images to file and path to database MYSQL

I have been bagging my head against this problem for a few days now and can't seem to find the right answer anywhere. Any help would be very much appreciated!

End goal: let the user upload up to 4 images. check for errors. upload image using filename into the users folder on in the file system (mkdir if doesn't already exist), store file path in the corresponding part of the SQL table.

I keep changing things to get parts of the result and have now coded myself into a big rut. Please let me know if you have any questions or can even suggest a better way to get to the end goal.

Thanks!!

SQL table: post_id, user_id, created, post_title, short_descrip, Long_descrip, image1, image1_cap, image2, image2_cap, image3, image3_cap, image4, image4_cap


HTML:

 <form method ='post' enctype='multipart/form-data' action='/posts/p_add'> <h1><label for = 'content'>CREATE POST:</label><br></h1> Post Title <input type='text' name='post_title'><br><br> Short Description <input type='text' name='shortDescrip'><br><br> Long Description<textarea name = 'longDescrip' id = 'longDescrip'></textarea> <br><br> <label for="image1">Image 1:</label> <input type="file" name="image1" id="image1"><br> Image Caption<input type='text' name='image1_cap'><br> <label for="image2">Image 2:</label> <input type="file" name="image2" id="image2"><br> Image Caption<input type='text' name='image2_cap'><br> <label for="image3">Image 3:</label> <input type="file" name="image3" id="image3"><br> Image Caption<input type='text' name='image3_cap'><br> <label for="image4">Image 4:</label> <input type="file" name="image4" id="image4"><br> Image Caption<input type='text' name='image4_cap'><br> <input type = 'Submit' value = 'POST'> </form> 


PHP:

 public function p_add(){ # associate post with the user $_POST['user_id'] = $this->user->user_id; # unix timestamp for created & modified $_POST['created'] = Time::now(); $filename = $_POST['filename']; # create file path for images $imgPath = '/uploads/images/'.$this->user->user_id.'/'; if (!file_exists($imgPath)){ mkdir($imgPath, 0777, true); } if ($_FILES["file"]["error"] == 0){ for($x=1; $x<=4 ; $x++){ //print_r($_FILES); # upload the user-chosen file and save to img file $image = Upload::upload($_FILES, 'uploads/images/', array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG"), $filename); print_r($image); # notify of error if($image == 'Invalid file type.') { echo "invalid file type"; } else { # add to DB $data = Array("image".$x => $image); DB::instance(DB_NAME)->insert('posts', $data); # resize the image and save again /*$imgObj = new Image($_SERVER["DOCUMENT_ROOT"].'/uploads/images/'.$image); $imgObj->resize(100,100,"crop"); $imgObj->save_image($_SERVER["DOCUMENT_ROOT"].'/uploads/images/'.$image);*/ } } }else{ # if there is an error let it be known echo "there has been an error"; } # insert into db DB::instance(DB_NAME)->insert('posts', $_POST); # feedback echo "Your post was added"; } 

Upload function from framework:

 public static function upload($file_obj, $upload_dir, $allowed_files, $new_file_name = NULL) { # Access first element in file_obj array (b/c we're dealing with single file uploads only) $key = key($file_obj); $original_file_name = $file_obj[$key]['name']; $temp_file = $file_obj[$key]['tmp_name']; $upload_dir = $upload_dir; # If new file name not given, use original file name if($new_file_name == NULL) $new_file_name = $original_file_name; $file_parts = pathinfo($original_file_name); $target_file = getcwd().$upload_dir . $new_file_name . "." . $file_parts['extension']; # Validate the filetype if (in_array($file_parts['extension'], $allowed_files)) { # Save the file move_uploaded_file($temp_file,$target_file); return $new_file_name . "." . $file_parts['extension']; } else { return 'Invalid file type.'; } } 

You have an error in indexname:

 if ($_FILES["file"])...

you have no <input name="file"> try to check

 if ($_FILES["image1"]["error"] == 0)

and rewrite your code in this case.

UPDATE:

Very interesting Upload function... Bad solution.

Try this:

$image = Upload::upload(array($_FILES['image'.$x]), 'uploads/images/', 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM