gumireddy
gumireddy

Reputation: 61

Php save file to specific location?

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

Upvotes: 3

Views: 29929

Answers (3)

Shri Rama Bhakta
Shri Rama Bhakta

Reputation: 81

Use file_put_contents();

$image = file_get_contents('http://www.blahblahblah.com/logo.gif');
file_put_contents('./myDir/myFile.gif', $image);

Upvotes: 6

Aur&#233;lien Grimpard
Aur&#233;lien Grimpard

Reputation: 964

$File is your path and filename, you can change folder from it (http://www.php.net/manual/en/function.fopen.php).

Upvotes: 0

Victor York
Victor York

Reputation: 1681

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 =).

Upvotes: 4

Related Questions