I have this code written in PHP, im using zend server on win 7 as localhost.
<?php
$dir = $_POST['link'];
// Create DOM from URL or file
require_once 'library/simple_html_dom.php';
$html = file_get_html($dir);
foreach($html->find('img') as $element){
$image = $element->src;
$imglink = $dir . $image;
$filename = $element->src;
$parsed_url = parse_url($filename, PHP_URL_PATH);
$basename = basename($filename).PHP_EOL;
$filenameOut = __DIR__ . "/images/" . $basename;
$pos = strpos($image, 'data:image');
if ($pos===false){
$parsing = parse_url($filename, PHP_URL_HOST);
if ($parsing != NULL){
file_put_contents($filenameOut, file_get_contents($image));
}
else{
file_put_contents($filenameOut, file_get_contents($imglink));
}
}
}
?>
And it gives me this error
file_put_contents(C:\Program Files (x86)\Zend\Apache2\htdocs/images/chrome-48.png ): failed to open stream: Invalid argument
Ive tried putting ways i've read from other questions from this site, but doesnt work.
Related
PHP on execute causing HTTP error 500. I have chmod -R 777 on driver_license.exe.
<?php
$agent= $_GET['agentid'];
$userid = $_GET['userid'];
$file = "/home/www/file/driver_license.exe";
$path = "/home/www/file/";
"
$newfile ="driver_license_userid_1.exe";
$file_to_save = $path . $newfile;
if (!copy($file, $file_to_save)) {
echo "FAILED";
}
else {
echo "<a href=https://download.example.com/{$newfile}>DOWNLOAD & INSTALL</a>";
}
1.You have extra " after $path = "/home/www/file/";.Remove that.
2.You forget to add quotes around href.Add that
So code need to be:-
<?php
$agent= $_GET['agentid'];
$userid = $_GET['userid'];
$file = "/home/www/file/driver_license.exe";
$path = "/home/www/file/";
//" need to remove or comment
$newfile ="driver_license_userid_1.exe";
$file_to_save = $path . $newfile;
if (!copy($file, $file_to_save)) {
echo "FAILED";
}
else {
echo "<a href='https://download.example.com/{$newfile}'>DOWNLOAD & INSTALL</a>"; // ' added around href
}
Important Note:- folders need to be permissioned for access (give 077 permission for home,www and file folders)
I am trying to use file_put_contents on my local website to download some images from a website and save them to the C:\ drive. However when running the script I get an error
file_put_contents(C:\product_images\A): failed to open stream: Permission denied
I have full permission for the product_images folder and also the A folder inside.
I know I could just chmod in ubuntu but not sure what I could do with Windows. I just right clicked and selected properties and made sure all the users had all the permissions applied
public function showImage() {
$product_image = ['/ABCD/ABCD.jpg','/ABCDE/ABCDE.jpg'];
foreach($product_image as $product_images) {
$url = "http://images.url.com/product_images" . $product_images ."";
$letter = substr($product_images, 1, 1);
$folder = 'C:\product_images\ ' .$letter . '';
$new_folder = str_replace(' ', '', $folder);
file_put_contents($new_folder, file_get_contents($url));
}
$success = "Success!";
return $success;
}
foreach($product_image as $product_images) {
$url = "http://images.com/product_images" . $product_images ."";
$test = explode("/", $product_images);
$folder = 'C:\product_images\ ' . $test[2] . '';
$new_folder = str_replace(' ', '', $folder);
$newer_folder = str_replace('/', '\\', $new_folder);
file_put_contents($newer_folder, file_get_contents($url));
echo '../product_images/' . $test[2] . '';
echo '<br />';
}
file_put_contents works with URLs only if allow_url_fopen is on ("1"). Check it with
ini_get("allow_url_fopen");
Also notice that while your writing folder might have the required permissions, the parent folder may NOT. Both (possible) folders should have the permissions.
You can also test the folder if its writable (through is_writable)
I'm trying to save the external images on my site
I have this code:
$file = $_GET['url'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/images/name.jpg';
if ( copy($file, $newfile) ) {
echo "Copy saved";
}else{
echo "copy failed";
}
it works well and save images on my server OK, but when I try to use inside FOR, i have this warning:
Warning: copy(): Filename cannot be empty in /home/animesad/public_html/bot/getimg.php on line 18
My code is:
<?php
require "../functions.php";
$p = $_GET['p'];
$url = "http://www.mysite.net/en/movie/page/".$p."/";
$source = curl_get_contents("$url");
preg_match_all('#<li title="(.*?)"><a href#',$source,$name); //outputs title post
preg_match_all('#<img src="(.*?)" width="140" height="200"/>#',$source,$img); //outputs img url
for($i=0;$i<10;$i++){
//print_r($name[1][$i]."<hr />"); //that code show the names OK. (Title posts);
$namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.
$newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';
if ( copy($file, $newfile) ) {
echo "copy saved <hr />";
}else{
echo "copy failed <hr />";
}
}
?>
Any idea is welcome.
you forgot your $file
$file = $_GET['url'];
for($i=0;$i<10;$i++){
$namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.
$newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';
if ( copy($file, $newfile) ) {
echo "copy saved <hr />";
}else{
echo "copy failed <hr />";
}
}
I am getting a warning.
Warning: fopen(76561197992146126 .txt): failed to open stream:
Invalid argument in C:\wamp\www\Download\t3.php on line 6
For anyone wondering, this is the contents of main.txt below (http://pastebin.com/53chSRRz)
<?php
$APIkey = 'APIKeyHere';
$file = file('C:\wamp\www\Download\main.txt');
foreach ($file as $link) {
$link2 = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $link . "&format=json";
$downloaded = file_get_contents($link2);
$fh = fopen($link . ".txt", "a"); //or die("can't open file");
fwrite($fh, $downloaded);
}
echo "Finished";
?>
If I replace "fopen($link . ".txt", "a")" with a static file-name it works. But I need $link to be the filename. It is imperative to my setup.
I am running Windows 7 x64 using WAMP 2.2 with PHP 5.4.3
Your link IDs have whitespace in them. Try adding $link = trim($link); before creating $link2.
$ref_id = mysql_insert_id();
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie.basename($_FILES['uploaded'][$ref_id.'.jpg']) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
Gives:
Warning: move_uploaded_file(iso_photo/afwijkingen) [function.move-uploaded-file]: failed to open stream: Is a directory
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php6LQCqY' to 'iso_photo/afwijkingen'
What might be wrong?
edited 4th line, should work now
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie . "/" . basename($_FILES['uploaded'][$ref_id]) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
but can't properly say, as i don't know content of $ref_id variable