2014-05-04 90 views
-1

我有一個ffmpeg轉換器,可將視頻轉換爲mp4。轉換後,我想刪除原始視頻以保存文件空間。 我嘗試使用unlink($file_path),但它說權限被拒絕。使用取消鏈接從服務器中刪除文件

到目前爲止,視頻轉換並生成轉換的縮略圖。

$ffmpeg = 'ffmpeg'; 
$output = dirname(__DIR__).'/uploads/thumbs/'.$_file.'.jpg'; 
$input = dirname(__DIR__).'/uploads/'.$file; 
$mov = new ffmpeg_movie($input); 
$d = $mov->getDuration(); 
$iscopy = $mov->getCopyright(); 
$h = $mov->getFrameHeight(); 
$w = $mov->getFrameWidth(); 
$pos = ceil((int)$d /3); 
$size = $w.'x'.$h; 
$i = explode('.',$input); 
$o = $i[0].'.mp4'; 
if(ceil($d) < 1200){ 
    if($ext != 'mp4'){ 
    $cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -strict -2 $o"; 
     shell_exec($cmd); 
    } 
    $cmd = "ffmpeg -ss $pos -i $o -an -s $size $output"; 
    shell_exec($cmd); 
    $total_time += $pos; 
    $succedeed[] = array('name' => $name,'file' => 'thumbs/'.$_file.'.jpg', 'type' => 'mp4');  

if(file_exists('../uploads/'.$file)){ 
           unlink('../uploads/'.$file); 
    }      

}else{ 
    $failed[] = array('name' => $name, 'file' => $file, 'error' => 'Video length cannot exceed 20mins.'); 
} 

該文件的路徑是這樣的:

unlink(../uploads/fdbc716e18173d4fe895c6d0b03365df1399237360.avi) 

我已經試過this (question)this (question)和大量的谷歌搜索,都沒有成功:

chdir($FilePath); // Comment this out if you are on the same folder 
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465 
$do = unlink($FileName); 

if($do=="1"){ 
    echo "The file was deleted successfully."; 
} else { echo "There was an error trying to delete the file."; } 

回答

1

如果我已經正確地讀出你的問題,下面應該工作:

<?php 
    $FileName = "uploaded_file" 
    if (unlink($FileName)) 
     { 
     echo ("$FileName has been deleted successfully. "); 
     } 
     else 
     { 
     echo ("The uploaded file has NOT been deleted."); 
     } 
?> 

如果事實並非如此。您將不得不在上傳過程中更改文件的所有權。

chmod($FileName, 0755); 
0

您需要在unlink函數中引用你的路徑。

$do = unlink('../uploads/fdbc716e18173d4fe895c6d0b03365df1399237360.avi'); 

除了我建議只是在做:

if($do) { 
    // true 
} else { 
    // false 
} 

$斷開鏈接只會返回true或false,所以你可以直接在if語句。

如果您可以SSH入服務器,請在上傳目錄的目錄中執行「ls -la」並查看擁有者和權限是什麼。如果他們通過php上傳,那麼他們應該由可以刪除它們的同一用戶擁有。

0

的file_exists函數不接受相對路徑,而不是使用絕對路徑:

$doc_root = $_SERVER['DOCUMENT_ROOT']; 
if(file_exists($doc_root . '/uploads/' . $file)){ 
    unlink($doc_root . '/uploads/' . $file); 
} 
0
// delete from folder 
$filename = 'test.txt'; 
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete. 
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root 
// your file will be removed from the folder 

只需修改這個代碼,它會爲你

工作
相關問題