[英]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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.