upload a file to my web directory - php

I'm trying to upload a file (png) to my web directory using an URL:
$url = "http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100";
$file = file_get_contents($url);
$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() . '/../web/uploads/img';
$file->move( $dir , $fileName);
Warning: file_get_contents(http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() .
'/../web/uploads/img'.$fileName;
$ch = curl_init('http://api.qrserver.com/v1/create-qr-code/?
data=hello_word&size=100x100');
$fp = fopen($dir, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Try something like that. If that doesn't work i'd consider using. http://php.net/manual/en/function.file-put-contents.php

Function file_get_contents will return a string.
You need to use http://php.net/manual/en/function.urlencode.php function to encode your URL before passing it to file_get_contents.
Check file_put_contents function in php.net docs.

Related

Get file contents without file_get_contents

I'm not allowed to use file_get_contents. Originally I got the file contents by simply doing this:
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filecontent = file_get_contents($filepath);
$encodedFile = base64_encode($filecontent);
Unfortunately, it's not allowed.
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filename = $_FILES['resume-attachment']['name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $filepath);
$filecontent = curl_exec($ch);
curl_close($ch);
$encodedFile = base64_encode($filecontent);
The code above is my attempt at using cURL. I get the filename uploaded, so I get some reaction, but the uploaded file is 0.0 bytes of size... which is not correct. I would think that perhaps the issue could be that I shouldn't treat $filepath as a URL, but what would the alternative be? I should also mention that I'm not trying to post it from this code. I simply want to get the file contents, and then encode it. It's part of an XML string later on.

file_put_contents is unable to get whole file

I am using hostmonster as hosting provider.
I have written below code.
$mp4_url = 'http://www.w3schools.com/html/mov_bbb.mp4';
$filename = 'sample.mp4';
file_put_contents( $filename , fopen($mp4_url, 'r' ) );
I am seeing strange behaviour. Sometimes it works but sometimes it copies only few bites like sometimes size of file is 100kb sometimes 600kb sometimes it copies whole file.
Please suggest what should I do to copy any mp4 file from any server to our server.
We have to copy large files, size can be 600MB or 1GB.
Try copy(). This will do your job:
$file = 'http://www.w3schools.com/html/mov_bbb.mp4';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/sample.mp4';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
use curl rather than using file_put_contents
$url='http://www.w3schools.com/html/mov_bbb.mp4';
$saveto='path';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);

Upload file from URL

I've been trying to figure out how to upload a file from a URL using the following code without using a form. In a form I can enter in a URL instead of a local file on the computer and it uploads it, So im guessing its possible?:
example location: http://www.bubblews.com/assets/images/news/521013543_1385596410.jpg
if ((isset($_FILES['upload']['name'])) && (substr_count($_FILES['upload']['type'],'image/'))) {
$prefix = "market/".$market->guid;
$filehandler = new ElggFile();
$filehandler->owner_guid = $market->owner_guid;
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('upload'));
$filehandler->close();
Try this example:
Saving image from PHP URL
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
or this
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
In linux hosts you can try
exec("wget ".$_POST['url']);

Copy img from url to server: no such file or directory

I am using php to get an image from url and copy it to my server but got an error saying there is no such file
Image example:
http://www.google.co.in/intl/en_com/images/srpr/logo1w.png
Here is the solution I am using:
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
I am getting the following error. Did i do anything wrong?
fopen(/location/to/save/image.jpg): failed to open stream: No such file or directory
✓ This worked for me.
Try it without the /location/to/save/
The file will be saved in the same folder you run the script in.
Such as:
<?php
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("image_google.jpg", "w");
fwrite($fp, $content);
fclose($fp);
?>
function download_image($url,$destination_path = '')
{
// CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else
{
$content = file_get_contents($url);
}
// CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
if(!file_exists($destination_path) && $destination_path != ''){
mkdir($destination_path, 0755, true);
}
// ATTEMPT TO CREATE THE FILE
$fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
fwrite($fp, $content);
fclose($fp);
}
download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
The folder (/location/to/save in here) should exist. You also need write permissions in it.

How to get a file from another server and rename it in PHP

I was looking for a way to perform a number of tasks in PHP
Get a file from another server
Change the file name and extention
Download the new file to the end user
I would prefer a method that acts as a proxy server type thing, but a file download would be fine
Thanks in advance
Try this
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path-to-file/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
After you save rename the file with whatever name you need
Refer this
http://www.php.net/manual/en/ref.curl.php
See the example at http://www.php.net/manual/en/function.curl-init.php
This grabs the data and outputs it straight to the browser, headers and all.
If you have allow_url_fopen set to true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
I use something like this:
<?php
$url = 'http://www.some_url.com/some_file.zip';
$path = '/path-to-your-file/your_filename.your_ext';
function get_some_file($url, $path){
if(!file_exists ( $path )){
$fp = fopen($path, 'w+');
fwrite($fp, file_get_contents($url));
fclose($fp);
}
}
?>

Categories