$fileType_array = array(IMAGETYPE_JPEG, IMAGETYPE_PNG);
$filenames = array($thumbnail_fieldname, $desktop_fieldname_1280x800, $desktop_fieldname_1366x768, $desktop_fieldname_1920x1080);
$files = array();
foreach($filenames as $filename){
if(isset($_FILES[$filename]['tmp_name'])){
$resource = getimagesize($_FILES[$filename]['tmp_name']);
$type = $resource[2];
if(in_array($type, $fileType_array, TRUE)){
echo "<p>Following files are images:";
array_push($files, $filename);
//$files[$filename] = $filename . "<br />";
}
}
此代码为我提供了图像文件,我想要做的是将这些图像移动到适当的文件夹...例如缩略图图像文件是thumbnail.png
,我想将此文件移动到images/thumbnails/thumbnail.png
文件夹..我可以这样做吗?我认为使用move_uploaded_file
但不确定如何因为约束条件,如果特定文件不是图像,请将其留下并转移到下一个...例如,下一个desktop1280x800
文件不是图像所以它将跳过并移动到下一个桌面文件,如果它是一个图像,则将其移动到目录。所以我无法按顺序完成我猜...更好的是移动那些文件的代码我只想要特定的一个是并非全部移动
if(move_uploaded_file($_FILES[$thumbnail_fieldname]['tmp_name'], $thumbnail_filename)
//move desktop files
&& in_array(TRUE, array(
move_uploaded_file($_FILES[$desktop_fieldname_1280x800]['tmp_name'], $desktop_filename_1280x800),
move_uploaded_file($_FILES[$desktop_fieldname_1366x768]['tmp_name'], $desktop_filename_1366x768),
move_uploaded_file($_FILES[$desktop_fieldname_1920x1080]['tmp_name'], $desktop_filename_1920x1080)))){
//Files moved.
}
答案 0 :(得分:1)
$type = $_FILES[$desktop_fieldname_1280x800]['type'];
if(strpos($type, 'image') !== false)
{
//code
}
替代
$filename_parts = explode('.', $_FILES[$desktop_fieldname_1280x800]['tmp_name']);
$ext = strtolower(end($filename_parts));
$autorized = array('png', 'jpeg', 'jpg', 'gif', 'bmp' ); // ...
if(in_array($ext, $autorized))
{
//code
}
或两者兼而有之。
EDIT
$fileType_array = array('image/jpeg', 'image/png');
$filenames = array('test');
$path_image = 'path/to/';
foreach($filenames as $filename){
if(isset($_FILES[$filename]['tmp_name'])){
$type = $_FILES[$filename]['type'];
if(in_array($type, $fileType_array)){
$name = $_FILES[$filename]["name"];
if(move_uploaded_file($_FILES[$filename]['tmp_name'], $path_image.$name)){
echo 'File moved';
}
else
{
echo 'Error';
}
}
else
{
echo 'File is not an image or format is not accepted';
}
}
}
?>
<form method='POST' action='index.php' enctype='multipart/form-data'>
<input type='file' name='test' />
<input type='submit' value='go' />
</form>