如何在图片上传功能中提供动态路径?

时间:2016-10-03 13:04:45

标签: php image image-uploading

我正在尝试使用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 = '';
   }
   ?>

1 个答案:

答案 0 :(得分:0)

您需要创建一个动态目录才能执行此操作。 只需在创建目标文件夹之前     的 move_uploaded_file

mkdir("images/website/'. $_POST[path].'");
$target_folder = "images/website/". $_POST['path']."/";