简体   繁体   English

如何将图像上传到文件夹

[英]How to upload images to folder

Can you please help me and tell me how I can get the images to be uploaded to a certain folder using the below script. 您能否帮助我,并告诉我如何使用以下脚本将图像上传到特定文件夹。

I want the full images to be saved to imagefolder and the thumbnails to be saved to inside the imagefolder. 我希望将完整图像保存到imagefolder,将缩略图保存到imagefolder内部。

The code is below. 代码如下。

<?php

$upload_image_limit = 5; // How many images you want to upload at once?
$upload_dir = ""; // default script location, use relative or absolute path
$enable_thumbnails = 1 ; // set 0 to disable thumbnail creation
$max_image_size = 1024000 ; // max image size in bytes, default 1MB

##################### THUMBNAIL CREATER FROM GIF / JPG / PNG

function make_thumbnails($updir, $img){

$thumbnail_width = 80;
$thumbnail_height = 60;
$thumb_preword = "thumb_";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);



if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; }
if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; }
if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; }


if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
$imgt($new_image,"$updir"."$thumb_preword"."$img");
}

}

################################# UPLOAD IMAGES

foreach($_FILES as $k => $v){

$img_type = "";

### $htmo .= "$k => $v<hr />"; ### print_r($_FILES);

if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < $max_image_size ){

$img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

$img_rname = $_FILES[$k]['name'];
$img_path = $upload_dir.$img_rname;

copy( $_FILES[$k]['tmp_name'], $img_path );
if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname);
$feedback .= "Image and thumbnail created $img_rname<br />";

}
}

############################### HTML FORM
while($i++ < $upload_image_limit){
$form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
}

$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" value="Upload Images!" style="margin-left: 50px;" />
</form>
';

echo $htmo;

?>

Thank you. 谢谢。

代码工作正常,要更改为其他文件夹,请更改此$upload_dir = "imagefolder";

if the code is ok, you need to change the variable $upload_dir, to a folder path with a 777 permissions (chmod) It will give permissions to the folder for the public, group and owner for execute, rewrite and read. 如果代码正确,则需要将变量$ upload_dir更改为具有777权限(chmod)的文件夹路径。它将为公用,组和所有者授予该文件夹的权限,以便执行,重写和读取。

example

$upload_dir="./imagefolder";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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