1
我想用PHP將圖像上傳到各自的文件夾。共有6個文件夾和2個文件夾都有子文件夾。我創建了一個包含文件夾和子文件夾列表的下拉列表。現在我可以創建縮略圖,但無法將原始圖像存儲在相應的文件夾中。我如何在函數中編寫動態路徑?如何在圖片上傳功能中提供動態路徑?
我想用下面的動態路徑:
$target_folder = "images/website/". $_POST["path"]."/";
,但我得到的錯誤了這條道路。
下面是cwUpload函數的以下代碼。
<?php
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;
//file name setup
$filename_err = explode(".",$_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count-1];
if($file_name != '')
{
$fileName = $file_name.'.'.$file_ext;
}
else
{
$fileName = $_FILES[$field_name]['name'];
}
//upload image path
$upload_image = $target_path.basename($fileName);
//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($upload_image);
$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
switch($file_ext){
case 'jpg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'jpeg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'png':
$source = imagecreatefrompng($upload_image);
break;
case 'gif':
$source = imagecreatefromgif($upload_image);
break;
default:
$source = imagecreatefromjpeg($upload_image);
}
imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
switch($file_ext){
case 'jpg' || 'jpeg':
imagejpeg($thumb_create,$thumbnail,100);
break;
case 'png':
imagepng($thumb_create,$thumbnail,100);
break;
case 'gif':
imagegif($thumb_create,$thumbnail,100);
break;
default:
imagejpeg($thumb_create,$thumbnail,100);
}
}
return $fileName;
}
else
{
return false;
}
}
if(!empty($_FILES['fileToUpload']['name'])){
//call thumbnail creation function and store thumbnail name
$upload_img = cwUpload('fileToUpload','images/','',TRUE,'images/thumbs/','200','160');
//full path of the thumbnail image
$thumb_src = 'images/thumbs/'.$upload_img;
//set success and error messages
$message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";
}else{
`//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}
?>
之前只需創建一個目標文件夾,請檢查相應的文件夾 – Elby
的權限,請添加一些明智的代碼縮進 – RiggsFolly
@Elby我檢查的權限。當我嘗試直接上傳圖片而不創建縮略圖時,我可以添加圖片。我只需要$ target_folder部分中的一個正確路徑 – HebleV