我创建了图片上传代码。首先创建目录,然后将图像上传到它。
在表单中我上传了3张图片。
我的代码是:
$dir_path = 'assets/images/product_images/'.$model;
mkdir($dir_path, 0777);
$i = 1 ;
foreach ($image as $new_image)
{
$dir_path_up = 'assets/images/product_images/'.$model."/";
$filename = $_FILES["$new_image"]["name"];
$image_name = $dir_path_up .$filename . $i. ".jpg";
echo $image_name;
$i++;
}
die();
回声结果
assets/images/product_images/255_2555/1.jpg
assets/images/product_images/255_2555/2.jpg
assets/images/product_images/255_2555/3.jpg
但是这些图片没有上传到目录。我回应了我创建的图像名称。它的alredy重新命名。那么为什么图像没有上传到目录中。
这有什么问题?
答案 0 :(得分:5)
从这里阅读手册CI image upload。
在codeigniter中,我们使用上传库上传图片。
根据您的要求传递参数
答案 1 :(得分:3)
使用PHP上传文件时,它会存储到临时文件夹中。 您可以使用$ _FILES在脚本中访问此文件,但它仍在您的临时文件夹中,将在下次请求时删除。
要保留上传的文件,您需要将其移至所需位置。
这个函数叫做move_uploaded_file(API:http://php.net/manual/en/function.move-uploaded-file.php)
bool move_uploaded_file ( string $filename , string $destination )
在你的情况下,它会导致类似:
$dir_path = 'assets/images/product_images/'.$model;
mkdir($dir_path, 0777);
$i = 1 ;
foreach ($image as $new_image)
{
$dir_path_up = 'assets/images/product_images/'.$model."/";
$filename = $_FILES["$new_image"]["name"];
$tmp_name = $_FILES["$new_image"]["tmp_name"]
$image_name = $dir_path_up .$filename . $i. ".jpg";
move_uploaded_file($tmp_name, $image_name);
echo $image_name;
$i++;
}
die();
答案 2 :(得分:1)
多张图片上传
可以正常使用<?php
$j = 0; // Variable for indexing uploaded image.
$target_path = 'assets/images/product_images/' . $last_id . '/'; // Declaring Path for uploaded images.
for ( $i = 0; $i < count($_FILES['image']['name']); $i++ )
{
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['image']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if ( ($_FILES["image"]["size"][$i] < 100000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)
)
{
move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_path);
}
else
{
}
}