Pregunta

I have a ffmpeg converter that converts videos into mp4's. After the conversion I want to delete the original video to save on file space. I tried using unlink($file_path) but it says permission denied.

So far the video converts and generates a thumbnail form the conversion.

$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.');
}

The path to the file would be something like this:

unlink(../uploads/fdbc716e18173d4fe895c6d0b03365df1399237360.avi)

I have tried this (question), this (question) and a lot of Googling, without success:

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."; } 
¿Fue útil?

Solución

If I have read your question correctly, the following should work:

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

If it does not. You will have to change the ownership of the files during upload.

 chmod($FileName, 0755);

Otros consejos

You will need to throw quotes around your path in the unlink function.

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

As well I recommend just doing:

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

$unlink only returns true or false so you can use in directly in the if statement.

If you can ssh into the server, do "ls -la" on your directory the uploads are in and see what the owner and permission ares. If they are uploaded via php, then they should be owned by the same user that can delete them.

The file_exists function does not take relative paths, use an absolute path instead:

$doc_root = $_SERVER['DOCUMENT_ROOT'];
if(file_exists($doc_root . '/uploads/' . $file)){
    unlink($doc_root . '/uploads/' . $file);
} 
// 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

just modify this code and it will work for you

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top