How can i save an uploaded image to a folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ellerforums
Forum Newbie
Posts: 1
Joined: Mon Feb 06, 2012 3:26 pm

How can i save an uploaded image to a folder?

Post by ellerforums »

i already wrote the move_uploaded_file function but it's not working...everything is working properly except that, please i really need help!!!!
<?php
if (isset($_POST['cathegory'] , $_POST['name'] , $_POST['description'] , $_FILES['image_1'] , $_FILES['image_2'] , $_FILES['image_3'])){
$name = $_POST['name'];
$cathegory = $_POST['cathegory'];
$description = $_POST['description'];
$image_1 = $_FILES['image_1'];
$image_2 = $_FILES['image_2'];
$image_3 = $_FILES['image_3'];
$image_1_name = $image_1['name'];
$image_2_name = $image_2['name'];
$image_3_name = $image_3['name'];
$image_1_tmp = $image_1['tmp_name'];

}

if (empty($name) or empty($description)) {
echo 'Complete all information please' ;

} elseif(substr($image_1['type'],0,5) != 'image' or substr($image_2['type'],0,5) != 'image' or substr($image_2['type'],0,5) != 'image') {
echo 'please upload images only';

} elseif (strlen($name > 55) or strlen($description > 255)){
echo 'Name of description field contains too many characters.';

} else {
echo 'Starting images process :D <br>' ;
echo $cathegory.'<br>' ;
//echo $image_1_name ;

$directory = "$cathegory/$name";
if (!is_dir ($directory)) {
//file mode
$mode = '0775';
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
chmod($directory, 0777);

move_uploaded_file($image_1_tmp , $directory.'/'.$image_1_name);

}



}

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can i save an uploaded image to a folder?

Post by Celauran »

This is working fine for me:

Code: Select all

if (!empty($_FILES))
{
    $dir = 'images/' . $_POST['category'] . '/' . $_POST['name'];
    foreach ($_FILES as $file)
    {
        if (is_dir($dir))
        {
            move_uploaded_file($file['tmp_name'], $dir . '/' . $file['name']);
        }
        else
        {
            echo "Attempting to create {$dir}";
            $made = mkdir($dir, 0777, TRUE);
            if ($made)
            {
                move_uploaded_file($file['tmp_name'], $dir . '/' . $file['name']);
            }
        }
    }
}
Post Reply