简体   繁体   English

PHP 复制文件的文件名中有空格

[英]PHP copy files have spaces in the filename

I can't find a way to copy a file with a space in filename with PHP.我找不到用 PHP 复制文件名中带有空格的文件的方法。 I tried replacing a space with "\ " but it doesn't work too.我尝试用"\ "替换空格,但它也不起作用。

PHP Warning: copy(/home/user/images/Honeycomb PHP 警告:复制(/home/user/images/Honeycomb
Cotton\ Polo\ 1.jpg): failed to open stream: No such file or directory Cotton\ Polo\ 1.jpg): 无法打开流: 没有这样的文件或目录

Spaces should not be a problem.空间应该不是问题。 This might be a permission issue.这可能是权限问题。 Does PHP have permission to access the file? PHP是否有权访问该文件?

Replace spaces with %20用 %20 替换空格

$file_path = '/home/user/images/Honeycomb Cotton Polo 1.jpg';
$file_path = preg_replace('/\s/i', '%20', $file_path);

$new_path = '/home/user/images/Honeycomb-Cotton-Polo-1-copy.jpg';

copy($file_path, $new_path);

Replace spaces with %20用 %20 替换空格

In your case : copy(/home/user/images/Honeycomb\%20Cotton\%20Polo\%201.jpg)在您的情况下:复制(/home/user/images/Honeycomb\%20Cotton\%20Polo\%201.jpg)

Do a file_exists() check first:先做一个file_exists()检查:

$fileToCopy = '/home/user/images/Honeycomb Cotton Polo 1.jpg';
$target     = '/var/www/images/';

if (file_exists($fileToCopy)) {
    if (is_readable($fileToCopy)) {
         if (is_writeable(dirname($target))) {
             copy($fileToCopy, $target);
         } else {
             die('You dont have permission to write into '.dirname($target));
         }
    } else {
         die('You dont have permission to read '.$fileToCopy);
    }
} else {
    die('File '.$fileToCopy.' does not exist');
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM