簡體   English   中英

PHP將4張圖片上傳到文件並發送到數據庫MYSQL的路徑

[英]PHP upload 4 images to file and path to database MYSQL

幾天來,我一直在努力解決這個問題,似乎在任何地方都找不到正確的答案。 任何幫助將不勝感激!

最終目標:讓用戶最多上傳4張圖片。 檢查錯誤。 使用文件名將圖像上傳到文件系統中的users文件夾中(如果尚未存在,則為mkdir),並將文件路徑存儲在SQL表的相應部分中。

我一直在改變東西以獲取部分結果,現在將自己編碼成粗魯的事。 如果您有任何疑問,或者可以提出達到最終目標的更好方法,請告訴我。

謝謝!!

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"; } 

從框架上傳功能:

 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.'; } } 

您在索引名中有一個錯誤:

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

您沒有<input name="file">嘗試檢查

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

並在這種情況下重寫您的代碼。

更新:

非常有趣的上傳功能...解決方法不好。

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM