无法使用上载类从PHP中的URL复制图像
我正在尝试用PHP创建一个upload类。这是我的第一个PHP类:无法使用上载类从PHP中的URL复制图像,php,class,oop,upload,Php,Class,Oop,Upload,我正在尝试用PHP创建一个upload类。这是我的第一个PHP类: //Create Class class Upload{ //Remote Image Upload function Remote($Image){ $Content = file_get_contents($Image); if(copy($Content, '/test/sdfsdfd.jpg')){ return "UPLOADED"; }else{ return
//Create Class
class Upload{
//Remote Image Upload
function Remote($Image){
$Content = file_get_contents($Image);
if(copy($Content, '/test/sdfsdfd.jpg')){
return "UPLOADED";
}else{
return "ERROR";
}
}
}
使用方法:
$Upload = new Upload();
echo $Upload->Remote('https://www.gstatic.com/webp/gallery/4.sm.jpg');
问题是,这门课不起作用。问题在哪里?我是新的PHP类,并试图学习它
谢谢。复制需要文件系统路径,例如
copy('/path/to/source', '/path/to/destination');
您正在传递获取的文字图像,因此
copy('massive pile of binary garbage that will be treated as a filename', '/path/to/destination');
你想要
file_put_contents('/test/sdfsdfg.jpg', $Content);
相反。复制需要文件系统路径,例如
copy('/path/to/source', '/path/to/destination');
您正在传递获取的文字图像,因此
copy('massive pile of binary garbage that will be treated as a filename', '/path/to/destination');
你想要
file_put_contents('/test/sdfsdfg.jpg', $Content);
PHP函数用于复制您有权复制的文件
由于您首先获取文件的内容,因此可以使用
PHP函数用于复制您有权复制的文件
由于您首先获取文件的内容,因此可以使用
既然您想下载图像,您也可以使用php的imagejpeg
-方法来确保以后不会出现任何损坏的文件格式():
- 将目标下载为“字符串”
- 从中创建图像资源李>
- 使用正确的方法将其另存为jpeg:
在您的方法中:
$content = file_get_contents($Image);
$img = imagecreatefromstring($content);
return imagejpeg($img, "Path/to/targetFile");
为了使file\u get\u contents
正常工作,您需要确保在php ini中将allow\u url\u fopen
设置为1
:
大多数托管主机在默认情况下禁用此功能。因此,请与技术支持联系,或者如果他们不启用allow\u url\u fopen
,您需要使用另一种尝试,例如使用cURL进行文件下载
您可以使用以下代码段检查其是否已启用:
if ( ini_get('allow_url_fopen') ) {
echo "Enabled";
} else{
echo "Disabled";
}
由于要下载图像,还可以使用php的imagejpeg
-方法确保以后不会出现任何损坏的文件格式():
- 将目标下载为“字符串”
- 从中创建图像资源李>
- 使用正确的方法将其另存为jpeg:
在您的方法中:
$content = file_get_contents($Image);
$img = imagecreatefromstring($content);
return imagejpeg($img, "Path/to/targetFile");
为了使file\u get\u contents
正常工作,您需要确保在php ini中将allow\u url\u fopen
设置为1
:
大多数托管主机在默认情况下禁用此功能。因此,请与技术支持联系,或者如果他们不启用allow\u url\u fopen
,您需要使用另一种尝试,例如使用cURL进行文件下载
您可以使用以下代码段检查其是否已启用:
if ( ini_get('allow_url_fopen') ) {
echo "Enabled";
} else{
echo "Disabled";
}
您所描述的是更多的下载(到服务器)然后上传
您所描述的是更多的下载(到服务器)然后上传
我需要修好这门课,我的朋友@Alireza我对它进行了编辑,以便您可以看到函数的外观。@Alireza哪个部分不工作?在我的机器上测试这个效果很好。你收到什么消息了吗?谢谢你的回答,但是在使用了文件内容功能之后,一切都好了,我需要修复这个类,我的朋友@Alireza我对它进行了编辑,以便您可以看到函数的外观。@Alireza哪个部分不工作?在我的机器上测试这个效果很好。你收到什么消息了吗?谢谢你的回答,但是在使用文件内容功能后,一切都好了