vep temp
vep temp

Reputation: 267

Move the images to specific directory

    $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 />"; 
                }

            }

This code gives me files that are images, what I would like to do is move those images to appropriate folders..for example for the thumbnail image the file is thumbnail.png, I would like to move this file to images/thumbnails/thumbnail.png folder..how can I do that? I think with move_uploaded_file but not sure how because of the constraint that if the specific file is not image leave it and move to the next one..for example the next one desktop1280x800 file is not an image so it will skip and move to the next desktop file to move it to the directory if it is an image..so i cannot be done in order I guess..below is the code which moves those file I just want the specific one's to be moved not all

    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.
}

Upvotes: 1

Views: 1228

Answers (1)

Tom Tom
Tom Tom

Reputation: 3698

 $type = $_FILES[$desktop_fieldname_1280x800]['type'];
 if(strpos($type, 'image') !== false)
 {
   //code
 }

Alternative

$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
}

Or a combination of both.

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>

Upvotes: 1

Related Questions