doumi0737 2014-05-08 05:49
浏览 56
已采纳

Cronjob没有按预期工作(php文件)

I have written a script for my customer which downloads youtube videos and uploads them to dailymotion. When I run dailyuploader.php with browser it works Ok.

Also when I run it from terminal;

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
dosya:/www/gokhan/uploads/27-nefes-fuibron-yar-dedim-sana-dj-karaduman-2013.mp4
Directory:/www/gokhan/uploads/27-nefes-fuibron-yar-dedim-sana-dj-karaduman-2013.mp4
Array
(
    [id] => x1tjwo8
    [title] => 27 Nefes & Fuibron - Yar Dedim Sana (Dj Karaduman 2013)
    [channel] => music
    [owner] => x1e8kjt
)
Basarili

It works Ok.

But when i add it to crontab It doesn't work as expected.

I get output;

X-Powered-By: PHP/5.4.28
Content-type: text/html

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
dosya:
Directory:
Dosya yok ya da 0kb

My cron command is like this: */20 * * * * /usr/bin/php-cgi /www/gokhan/dailyuploader.php >> /www/gokhan/out.txt

Also I use commands that needs high permissions: rename, (exec with youtube-dl: an application that downloads youtube videos) etc.

Also I have even tried to make chmod 777 all files and directories. Also I have changed owner of files to root but result was same.

Any help appreciated.

Edit.1 function that downloads video.

function download_video($videoid){
    exec('youtube-dl '. $videoid .' -o "/www/gokhan/uploads/%(title)s.mp4"');
    return true;
}

function that gets video path and title to upload.

function get_file(){
$dir = opendir("/www/gokhan/uploads/");
while (($dosya = readdir($dir)) !== false)
{
    if(! is_dir($dosya)){
        $title = substr($dosya,0,-4);
        $direc = "/www/gokhan/uploads/";
        $t = seola(substr($dosya,0,-3)).".mp4";
        rename($direc.$dosya, $direc.$t);
        $tabi = $direc.$t;
    }
}
closedir($dir);
echo "dosya:".$tabi."
";
$ar['dosya'] = $tabi;
$ar['title'] = $title;
return $ar;
}

function that uploads file to dailymotion

function upload_to_dailymotion($username,$password,$dir,$title){
include('/www/gokhan/Dailymotion.php');
require_once '/www/gokhan/Dailymotion.php';
echo "Directory:".$dir."
";
$apiKey = "apikey";
$apiSecret = "apisecret";
$api = new Dailymotion();
$api->setGrantType(Dailymotion::GRANT_TYPE_PASSWORD, $apiKey, $apiSecret, array('write','delete'),
array('username' => $username, 'password' => $password));
if(file_exists($dir) and filesize($dir) > 0){
    $url = $api->uploadFile($dir);
    $result1 = $api->post('/me/videos', array('url' => $url, 'title' => $title , 'description' => '' , 'channel' => 'music', 'tags' => '', 'published' => true));
    print_r($result1);
    $url = $result1['id'];
    return $url;
}
else
    die("Dosya yok ya da 0kb");
}

Edit.2 Function that connects all methods obove..

function download_get_link($videoid,$username,$password){
$indirme = download_video($videoid);
$ar= get_file();
$title = $ar['title'];
$dir = $ar['dosya'];
if($indirme){
    $url = upload_to_dailymotion($username,$password,$dir,$title);
    $res['url'] = $url;
    $res['title'] = $title;
    $res['description'] = '';
    return $res;
}
  • 写回答

1条回答 默认 最新

  • douzhaolu4839 2014-05-08 10:37
    关注

    Running scripts via a web browser results in all paths being relative to the scripts location. When running them from command line they're relative to your current working directory.

    So when you run your script like this:

    /var$ php www/dir/script.php
    

    it won't work either.

    You use absolute paths in most parts of your script, but not in your get_file function:

    if(!is_dir($dosya)) {
    

    That should be:

    $direc = "/www/gokhan/uploads/";
    
    while (($dosya = readdir($dir)) !== false) {
        if(!is_dir($direc.$dosya)){
            $title = substr($dosya, 0, -4);
            $t = seola(substr($dosya, 0, -3)).".mp4";
            rename($direc.$dosya, $direc.$t);
            $tabi = $direc.$t;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?