通过 PHP 上传多张图片
Uploading Multiple images via PHP
下面是新代码。我使用这个 oop PHP 它正在工作我在这里添加它是因为我删除了我的旧版本的问题是不好的。如果有人需要这种代码是可用的。
public function set_file($file) {
if(empty($file) || !$file || !is_array($file)) {
$this->errors[] = "There was no file uploaded here";
return false;
}elseif($file['error'] !=0) {
$this->errors[] = $this->upload_errors_array[$file['error']];
return false;
} else {
$this->user_image = basename($file['name']);
$this->tmp_path = $file['tmp_name'];
$this->type = $file['type'];
$this->size = $file['size'];
}
}
public function move_image(){
$target_path = SITE_ROOT.DS. 'admin' . DS . $this->upload_directory . DS . $this->user_image;
return move_uploaded_file($this->tmp_path, $target_path);
}
尝试将文件的输入名称属性指定为数组:
<input type="file" id="gallery_album_image" multiple name="uploaded_file[]" >
然后您必须使用 $_FILES 重构代码。该值将是数组:
$_FILES["uploaded_file"]["name"][0..num of images]
$_FILES["uploaded_file"]["tmp_name"][0..num of images]
您也可以使用此 foreach 循环或任何需要的人。
// Configure upload directory and allowed file types
$upload_dir = 'images_uploads'.DIRECTORY_SEPARATOR;
$allowed_types = array('jpg', 'png', 'jpeg', 'gif');
// Define maxsize for files i.e 2MB
$maxsize = 4 * 1024 * 1024;
// Checks if user sent an empty form
if(!empty(array_filter($_FILES['files']['name']))) {
// Loop through each file in files[] array
foreach ($_FILES['files']['tmp_name'] as $key => $value) {
$file_tmpname = $_FILES['files']['tmp_name'][$key];
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
$file_name = time().rand(). $file_name.".".$file_ext;
// Set upload file path
$filepath = $upload_dir.$file_name;
$sql = "INSERT INTO gallery_gallery(gallery_album_id, gallery_images) VALUES(:gallery_album_id,:file_name) ";
$values = [
':gallery_album_id' => $gallery_album_id,
':file_name' => $file_name
];
$query =$connection->prepare($sql);
$query->execute($values);
// Check file type is allowed or not
if(in_array(strtolower($file_ext), $allowed_types)) {
// Verify file size - 2MB max
if ($file_size > $maxsize)
echo "Error: File size is larger than the allowed limit.";
// If file with name already exist then append time in
// front of name of the file to avoid overwriting of file
if(file_exists($filepath)) {
$filepath = $upload_dir.time().$file_name;
if( move_uploaded_file($file_tmpname, $filepath)) {
echo "{$file_name} successfully uploaded <br />";
}
else {
echo "Error uploading {$file_name} <br />";
}
}
else {
if( move_uploaded_file($file_tmpname, $filepath)) {
echo "{$file_name} successfully uploaded <br />";
}
else {
echo "Error uploading {$file_name} <br />";
}
}
}
else {
// If file extention not valid
echo "Error uploading {$file_name} ";
echo "({$file_ext} file type is not allowed)<br / >";
}
}
下面是新代码。我使用这个 oop PHP 它正在工作我在这里添加它是因为我删除了我的旧版本的问题是不好的。如果有人需要这种代码是可用的。
public function set_file($file) {
if(empty($file) || !$file || !is_array($file)) {
$this->errors[] = "There was no file uploaded here";
return false;
}elseif($file['error'] !=0) {
$this->errors[] = $this->upload_errors_array[$file['error']];
return false;
} else {
$this->user_image = basename($file['name']);
$this->tmp_path = $file['tmp_name'];
$this->type = $file['type'];
$this->size = $file['size'];
}
}
public function move_image(){
$target_path = SITE_ROOT.DS. 'admin' . DS . $this->upload_directory . DS . $this->user_image;
return move_uploaded_file($this->tmp_path, $target_path);
}
尝试将文件的输入名称属性指定为数组:
<input type="file" id="gallery_album_image" multiple name="uploaded_file[]" >
然后您必须使用 $_FILES 重构代码。该值将是数组:
$_FILES["uploaded_file"]["name"][0..num of images]
$_FILES["uploaded_file"]["tmp_name"][0..num of images]
您也可以使用此 foreach 循环或任何需要的人。
// Configure upload directory and allowed file types
$upload_dir = 'images_uploads'.DIRECTORY_SEPARATOR;
$allowed_types = array('jpg', 'png', 'jpeg', 'gif');
// Define maxsize for files i.e 2MB
$maxsize = 4 * 1024 * 1024;
// Checks if user sent an empty form
if(!empty(array_filter($_FILES['files']['name']))) {
// Loop through each file in files[] array
foreach ($_FILES['files']['tmp_name'] as $key => $value) {
$file_tmpname = $_FILES['files']['tmp_name'][$key];
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
$file_name = time().rand(). $file_name.".".$file_ext;
// Set upload file path
$filepath = $upload_dir.$file_name;
$sql = "INSERT INTO gallery_gallery(gallery_album_id, gallery_images) VALUES(:gallery_album_id,:file_name) ";
$values = [
':gallery_album_id' => $gallery_album_id,
':file_name' => $file_name
];
$query =$connection->prepare($sql);
$query->execute($values);
// Check file type is allowed or not
if(in_array(strtolower($file_ext), $allowed_types)) {
// Verify file size - 2MB max
if ($file_size > $maxsize)
echo "Error: File size is larger than the allowed limit.";
// If file with name already exist then append time in
// front of name of the file to avoid overwriting of file
if(file_exists($filepath)) {
$filepath = $upload_dir.time().$file_name;
if( move_uploaded_file($file_tmpname, $filepath)) {
echo "{$file_name} successfully uploaded <br />";
}
else {
echo "Error uploading {$file_name} <br />";
}
}
else {
if( move_uploaded_file($file_tmpname, $filepath)) {
echo "{$file_name} successfully uploaded <br />";
}
else {
echo "Error uploading {$file_name} <br />";
}
}
}
else {
// If file extention not valid
echo "Error uploading {$file_name} ";
echo "({$file_ext} file type is not allowed)<br / >";
}
}