简体   繁体   English

如何将不带扩展名的文件从远程URL(应用程序的Google Play商店映像)复制到PHP中的服务器的临时文件夹?

[英]How to copy file without extension from remote url (Google Play Store image of app) to server's temp folder in php?

I am working on application of scraping structured data from Google Playstore and saving it in the database. 我正在研究从Google Playstore抓取结构化数据并将其保存在数据库中的应用程序。 I am getting all the structured data. 我正在获取所有结构化数据。 I am trying to copy the itemprop="image" url and save the image to my server. 我正在尝试复制itemprop="image"网址并将图像保存到我的服务器。

I have tried many things but nothing works as the file doesn't have an extension. 我已经尝试了很多方法,但是没有任何作用,因为该文件没有扩展名。 The code below works but the either invalid file is generated or a file with zero bytes is created. 下面的代码有效,但是要么生成了无效文件,要么创建了零字节文件。

Sample url that I am trying to copy 我要复制的示例网址

https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw

PHP code that I am using 我正在使用的PHP代码

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);   
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER,false);    
    // curl_setopt($process, CURLOPT_SSL_VERIFYPEER,0); 
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 
$image="https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw";
$upload_dir = wp_upload_dir();
$length= strlen($image);
$new_string = substr($image,0,$length-8);
$imagename= basename($new_string);
$image2 = getimg($imgurl); 
$new_image_path = file_put_contents($upload_dir['basedir'].'/custom-temp/'.$imagename,$image2); 

There are several errors in your code such as the $useragent not being named consistently and $imgurl being undefined. 您的代码中存在几个错误,例如$useragent名称不一致,而$imgurl的名称未定义。 Consider turning on errors in PHP when you are debugging so that you can catch these. 考虑在调试时打开PHP中的错误,以便可以捕获这些错误。

I can get your code to work like this (just change the path back to wp_upload_dir() ): 我可以使您的代码像这样工作(只需将路径更改回wp_upload_dir() ):

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $gim = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    // curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);   
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER,false);    
    // curl_setopt($process, CURLOPT_SSL_VERIFYPEER,0); 
    $return = curl_exec($process);
    curl_close($process);         
    return $return;     
} 
$image="https://lh3.googleusercontent.com/IYZe0LQOUKXpEYOyVOYYMJo4NnqBnDYkkhDgfYTgDCpuxAyy1ziBkOn0b6_LZxQ3qI4=w300-rw";
$upload_dir = "/home/laurent/test/upload";
$length= strlen($image);
$new_string = substr($image,0,$length-8);
$imagename= basename($new_string);
$image2 = getimg($image); 
$new_image_path = file_put_contents($upload_dir.'/'.$imagename,$image2); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP中使用curl复制远程图像(不带文件扩展名) - How to copy a remote image(without file type extension) using curl in PHP PHP / Java:如何将文件从一个文件夹复制到远程服务器内的另一个文件夹 - PHP/Java: How to copy the file from one folder into another folder inside the remote server PHP copy()方法与来自远程服务器的图像文件的可靠性 - Reliability of PHP copy() method with image file from remote server PHP将图像从url复制到文件夹 - PHP copy image from url to folder 将文件从远程URL复制到我的服务器 - Copy file from remote URL to my server 直接从 URL 下载文件到本地文件夹或远程服务器中的文件夹 - Download file directly from URL to local folder or a folder in remote server 如何使用 .htaccess 文件从服务器和本地主机上的 URL 中删除 .php 扩展名? - How to remove .php extension from URL on server and on localhost with .htaccess file? 仅从php中的url获取文件夹名称,而没有带有扩展名的任何文件名 - Get only folder name from url in php without any file name with extension 无需下载Google App引擎即可获取远程URL的文件大小(php) - Get file size of remote url without downloading in Google app engine(php) 如何使用php从没有FTP的远程服务器读取文件? - How to read file from remote server without FTP, using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM