2

I've got an demo page with jQuery File Upload that is currently allowing upload of video files to the web hosting through PHP.

Code:

<?
// A list of permitted file extensions
$allowed = array('mov', 'mp4', 'avi');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
exit;
?>

I need this demo to be fully working to upload video files to my Wistia gallery through their API instead of upload directory.

Working snippet for upload.php to Wistia API with video url:

<?
$data = array(
    'api_password' => '[password]',
    'project_id' => '[project_id]',
    'url' => '[video_url]'
);

$wistia = curl_init('https://upload.wistia.com');
curl_setopt_array($wistia, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS => http_build_query($data)
));

// Send the request
$wistia_request = curl_exec($wistia);
?>

However changing these values and using it in my form doesn't work:

$data = array(
    'api_password' => '[password]',
    'project_id' => '[project_id]',
    'file' => '@' . $_FILES['upl']['name']
);

As you can see I need guidance and help. Any hints are much appreciated.

Here's some docs for this project:

http://wistia.com/doc/upload-api

https://github.com/blueimp/jQuery-File-Upload

shahin8r
  • 113
  • 2
  • 7
  • "However changing these values and using it in my form doesn't work:" What is it that does not work? What kind of errors do you get? – Artmann Apr 28 '15 at 13:14
  • You could take another aproach...why don't you just upload your files to your server, send to the wistia api your file url, and once wistia is ok, delete the file from your upload folder... – Hackerman Apr 28 '15 at 17:24
  • Hey @Artmann. :) I get internal server error as response from Wistia API. – shahin8r Apr 28 '15 at 19:55
  • @RobertRozas the thought has crossed my mind, would rather avoid it since I have to pay extra storage and data fees, especially when it's large video files. – shahin8r Apr 28 '15 at 19:57
  • And if you use `'file' => '@' . $_FILES['upl']` instead? – Hackerman Apr 28 '15 at 20:05
  • @RobertRozas same error I'm afraid. – shahin8r Apr 28 '15 at 20:07
  • @shahin8r Well if you get a 500 response from Wistia there should be an usable error message to, otherwise it's probably best to contact their support. – Artmann Apr 28 '15 at 22:02

2 Answers2

1

Solved!

$data = [
   'file' => "@{$_FILES['upl']['tmp_name']};filename={$_FILES['upl']['name']};type={$_FILES['upl']['type']}"
]
shahin8r
  • 113
  • 2
  • 7
0

If you are going to upload the file directly there is no reason to move the uploaded file. You could probably just use the tmp-file directly.

$data= [
  'file' => '@' . $_FILES['upl']['tmp_name']
]

The next problem is that you are now doing 2 video uploads. 1 from the users computer to your server and then one from your server to Wistia. So instead of letting the user wait for 2 uploads you should move the second one into a background task.

Artmann
  • 130
  • 3
  • 14
  • Is User->Server->Wistia the only way or is User->Wistia possible? The User->Server and Server->Wistia is something I could do but would rather avoid. – shahin8r Apr 29 '15 at 14:21
  • Well it's possible you could submit the video file directly to Wistia via javascript but the problems is that it would expose your Wistia Credentials to the world and a bunch of CORS issues. – Artmann Apr 29 '15 at 21:37
  • True. I've uploaded now just with html and hidden input with api_password and it works, but not very practical. Can't really understand why it doesn't work with PHP when it works fine with curl. – shahin8r Apr 30 '15 at 07:53