上传同名文件
upload a file with the same name
我想从我的 android 应用程序上传文件。第一次成功,但第二次如果我想替换文件,它不会做任何事情。
<?php
$file_path = "usuario/";
$file_path = $file_path . basename( $_FILES['uploaded_imagen']['name']);
if(file_exists($file_path))
{
unlink($file_path);
}
if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $file_path)) {
echo "success1";
} else{
echo "fail1";
}
?>
您可以尝试在删除现有文件之前更改文件权限(如果您的 www 用户被允许这样做)
<?php
$basename = basename($_FILES['uploaded_imagen']['name']);
//todo: (play safe) $basename = hash('sha256', $basename) . '.EXT';
$target_file = 'usuario/' . $basename;
if(file_exists($target_file))
{
chmod($target_file,0755); //change perms if possible
unlink($target_file); //remove the file
}
if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $target_file))
{
echo "<P>FILE UPLOADED TO: $target_file</P>";
}
else
{
echo "<P>MOVE UPLOADED FILE FAILED!</P>";
print_r(error_get_last());
}
或者 - 只需更改目标目录的权限(首先找出您的服务器的 www 用户是什么 运行 之后):
ps aux | egrep '(apache|httpd)'
chown www-user:www-group '/path/to/files'
chmod 0700 '/path/to/files'
我想从我的 android 应用程序上传文件。第一次成功,但第二次如果我想替换文件,它不会做任何事情。
<?php
$file_path = "usuario/";
$file_path = $file_path . basename( $_FILES['uploaded_imagen']['name']);
if(file_exists($file_path))
{
unlink($file_path);
}
if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $file_path)) {
echo "success1";
} else{
echo "fail1";
}
?>
您可以尝试在删除现有文件之前更改文件权限(如果您的 www 用户被允许这样做)
<?php
$basename = basename($_FILES['uploaded_imagen']['name']);
//todo: (play safe) $basename = hash('sha256', $basename) . '.EXT';
$target_file = 'usuario/' . $basename;
if(file_exists($target_file))
{
chmod($target_file,0755); //change perms if possible
unlink($target_file); //remove the file
}
if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $target_file))
{
echo "<P>FILE UPLOADED TO: $target_file</P>";
}
else
{
echo "<P>MOVE UPLOADED FILE FAILED!</P>";
print_r(error_get_last());
}
或者 - 只需更改目标目录的权限(首先找出您的服务器的 www 用户是什么 运行 之后):
ps aux | egrep '(apache|httpd)'
chown www-user:www-group '/path/to/files'
chmod 0700 '/path/to/files'