Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php save file to specific location?

Tags:

php

i have this

<?php
$canvasImg = $_POST['img'];    
$data = base64_decode($canvasImg);
$File = $_POST['imgname'].jpg; 
$Handle = fopen($File, 'w');
fwrite($Handle, $data);  
fclose($Handle);
?>

this saves image.jpg to my theme root folder. how to save it to server root folder ... /Public_html/wp-content/uploads ?

thanks

like image 867
gumireddy Avatar asked Sep 16 '25 11:09

gumireddy


2 Answers

Use file_put_contents();

$image = file_get_contents('http://www.blahblahblah.com/logo.gif');
file_put_contents('./myDir/myFile.gif', $image);
like image 139
Shri Rama Bhakta Avatar answered Sep 19 '25 01:09

Shri Rama Bhakta


Try this:

<?php
  $link= $_POST['img'];
  $destdir = '/public_html/wp-content/uploads';
  $img=file_get_contents($link);
  file_put_contents($destdir.substr($link,strrpos($link,'/')),$img);
?>

Let me know if it worked out for you =).

like image 29
MONZTAAA Avatar answered Sep 19 '25 03:09

MONZTAAA