'PHP : Updating new image still showing the old image
So I am creating a new folder everytime if folder not exists using mdkir.
$dir_name = '../../assets/contestant_double/'.$event_id.'eventPortfolio/';
if(!is_dir($dir_name)){
mkdir('../../assets/contestant_double/'.$event_id.'eventPortolfio/', 0777, true);
}
Now I am trying to update my uploaded image on that folder. And its working fine when i look on that designated directory
$static_name = $event_id."d".$getThed1d2."_contestant_";
$extension = pathinfo($name, PATHINFO_EXTENSION);
$theRealName = $static_name.$lastDigit.".".$extension;
echo $theRealName;
But when i try to get the image why is it still showing up the old image instead the new one?
<img class="img_here_edit" src="../assets/contestant_double/'.$event_id.'eventPortfolio/'.$soc_sql['images'].'" width="100%" >
I doubt but I'm not sure that its on my new created directory mdkir which has 0777 because those of my code are working fine in my other regular folder? How do i solve this
Solution 1:[1]
use unlink(''); function to old image it will solve your problem
Solution 2:[2]
Why not delete the old image and create and upload a new image with the same name?.
Solution 3:[3]
Try sending a random id at the end of image url to get the latest image like this :
<img class="img_here_edit" src="../assets/contestant_double/'.$event_id.'eventPortfolio/'.$soc_sql['images'].'?'.time().'" width="100%" >
Solution 4:[4]
what you see is the picture on browser cache, so you have to clear cache of browser. however, you wont do that every time you replace a picture either your users, it is too tedious and unpractical. I had same sutation and what i did was the follown. create filename + time().jpg like this. So by the time i was going to replace old picture, i loaded from folder, delete it an then place new one. look...
function uploadphoto( $id, $ref ) {
$find = $ref.'_';
$data = $_POST[ 'image' ];
list( $type, $data ) = explode( ';', $data );
list( , $data ) = explode( ',', $data );
$data = base64_decode( $data );
$imageName = $ref.'_'.time().'.jpg'; // new picture name
foreach(glob('uploads/'.$find.'*.*') as $filename){
$localizacion = $_SERVER['DOCUMENT_ROOT'].'/'. $filename ;
chmod('./uploads', 0777);
unlink($localizacion);
}
file_put_contents( 'uploads/' . $imageName, $data );
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prudhvi Krishna V |
| Solution 2 | Alexander Vestling |
| Solution 3 | Gandharv |
| Solution 4 |
