1

I am want to upload image into following folder structure:

/var/www/html/project/public/upload/userimage/2016/04/07/thumbnail/image.png
                                               ^   ^  ^
                                            Year Month Date

and I have wrote this code:

$Day = date('d'); // Get date
$Month = date('m'); // Get month
$Year = date('Y'); // Get year

$data = $_POST['base64']; // Get base64 image data

$imageName = hash('ripemd160', time()).'.png';

if (!file_exists(public_path().'/upload/userimage/'.$Year.'/'.$Month.'/'.$Day.'/thumbnail')) {
    mkdir(public_path().'/upload/userimage/'.$Year.'/'.$Month.'/'.$Day.'/thumbnail', 0777, true);
}
$url = public_path().'/upload/userimage/'.$Year.'/'.$Month.'/'.$Day.'/thumbnail/'.$imageName;

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents($url.'/'.$imageName, $data);

and getting this error:

file_put_contents(/var/www/html/project/project/public/upload/userimage/2016/04/07/thumbnail/8b38eb25ce97d428afcd80d6ddcd16b8ca266a52.png/8b38eb25ce97d428afcd80d6ddcd16b8ca266a52.png): failed to open stream: No such file or directory

I just want to upload image to directory folder with generating folder Year -> Month -> Date if Year/Month/Date is changed.

I will appreciate if you help me to make my code more proper :)

Thanks.

1
  • I dunno whether it works, but once try creating the directory first using mkdir function and then upload files into it. Commented Apr 7, 2016 at 9:25

2 Answers 2

1

Yes, your directory doesn't exists.

$url = public_path().'/upload/userimage/'.$Year.'/'.$Month.'/'.$Day.'/thumbnail/'.$imageName;
#                                                                                 ----------
file_put_contents($url.'/'.$imageName, $data);
#                          ----------

You create this directory:

/My/New/Directory

Then you try to write this file:

/My/New/Directory/Image.jpg/Image.jpg

Use this command:

file_put_contents( $url , $data );

A general suggestion: use variables instead of repeat concatenate patterns, especially if you have long concatenations. Your code is more clear, code maintenance is easier and the risk of typos is lower:

$dirPath = public_path().'/upload/userimage/'.$Year.'/'.$Month.'/'.$Day.'/thumbnail';
if( !file_exists( $dirPath ) )      
    mkdir( $dirPath, 0777, true );
}
$filePath = $dirPath.'/'.$imageName;
(...)
file_put_contents( $filePath , $data );
Sign up to request clarification or add additional context in comments.

Comments

1

file_put_contents does not create directory: So use:

if (!is_dir('upload/usersimage/'.$Year.'/'.$Month.'/'.$Day.'' . $month)) {
    mkdir('upload/usersimage/'.$Year.'/'.$Month.'/'.$Day.' . $month);
}

See here: Creating a folder when I run file_put_contents()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.