How to save image in a subdirectory using file_put_contents and return the path in php

391 Views Asked by At

I am new to PHP but have experience in .net. I need to know a way to save images uploaded to a PHP page in a subdirectory of that page and return its path. I have checked and not found a way to give permission to an existing subdirectory in PHP. I am using wamp for development.

  1. passed subdirectory in file_put_contents with the image file name.
  2. got full path and appended the subdirectory.
$ImagePath = __DIR__.'/driverimages/'.$ImageName ;
file_put_contents($ImagePath, base64_decode($data->profilepic));

I know that if I create a directory from a PHP script with 0774 permission I can write to it. But I need to write to an existing subdirectory and return the image path so that it can be used in the front end to show the image.

I need to be able to write to a subdirectory from the current directory and return the path of the written image file.

1

There are 1 best solutions below

2
dılo sürücü On

$ImagePath = __DIR__.'/driverimages/'.$ImageName;

mkdir(dirname($ImagePath),777);

file_put_contents($ImagePath, base64_decode($data->profilepic));