1
    $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.
}
4

1 回答 1

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
}

或两者兼而有之。

编辑

  $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>
于 2013-11-15T01:42:45.070 回答