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

时间:2014-05-08 05:49:03

标签: php cron crontab

我为我的客户编写了一个脚本,可以下载YouTube视频并将其上传到dailymotion。 当我使用浏览器运行dailyuploader.php时,它可以正常工作。

当我从终端运行时;

<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

一切正常。

但是当我将它添加到crontab时它没有按预期工作。

我得到输出;

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

我的cron命令是这样的:     * / 20 * * * * / usr / bin / php-cgi /www/gokhan/dailyuploader.php&gt;&gt; /www/gokhan/out.txt

此外,我使用需要高权限的命令:rename,(exec with youtube-dl:一个下载youtube视频的应用程序)等。

此外,我甚至尝试使chmod 777所有文件和目录。 此外,我已将文件的所有者更改为root,但结果相同。

任何帮助表示感谢。

Edit.1 下载视频的功能。

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

获取上传视频路径和标题的功能。

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."\n";
$ar['dosya'] = $tabi;
$ar['title'] = $title;
return $ar;
}

将文件上传到dailymotion的功能

function upload_to_dailymotion($username,$password,$dir,$title){
include('/www/gokhan/Dailymotion.php');
require_once '/www/gokhan/Dailymotion.php';
echo "Directory:".$dir."\n";
$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 连接所有方法的函数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 个答案:

答案 0 :(得分:1)

通过Web浏览器运行脚本会导致所有路径都与脚本位置相关。从命令行运行它们时,它们相对于您当前的工作目录。

所以当你像这样运行你的脚本时:

/var$ php www/dir/script.php

它也不会工作。

您在脚本的大多数部分使用绝对路径,但在get_file函数中不使用

if(!is_dir($dosya)) {

应该是:

$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;
    }
}