mysqldump common install locations for mac/linux - php

I am trying to know all the common locations for mysqldump. The list I have come up with is as follows:
'/usr/bin/mysqldump', //Linux
'/usr/local/mysql/bin/mysqldump', //Mac OS X
'/usr/local/bin/mysqldump', //Linux
'/usr/mysql/bin/mysqldump'; //Linux
Often mysqldump isn't in the path, so I am trying to have all the locations to look in. (I am running this from a php script)
Are there any that I am missing?

I was unable to find any other paths apart from the ones you have given in your question. However, one thing that does come in my mind is that mysqldump should, in most cases, be in the same directory as the mysql binary. Now, the mysql command will be in the path, in most cases, as well.
And, therefore, you can combine the two logics to have the location of the mysqldump binary, in most cases, like this:
function detect_mysqldump_location() {
// 1st: use mysqldump location from `which` command.
$mysqldump = `which mysqldump`;
if (is_executable($mysqldump)) return $mysqldump;
// 2nd: try to detect the path using `which` for `mysql` command.
$mysqldump = dirname(`which mysql`) . "/mysqldump";
if (is_executable($mysqldump)) return $mysqldump;
// 3rd: detect the path from the available paths.
// you can add additional paths you come across, in future, here.
$available = array(
'/usr/bin/mysqldump', // Linux
'/usr/local/mysql/bin/mysqldump', //Mac OS X
'/usr/local/bin/mysqldump', //Linux
'/usr/mysql/bin/mysqldump' //Linux
);
foreach($available as $apath) {
if (is_executable($apath)) return $apath;
}
// 4th: auto detection has failed!
// lets, throw an exception, and ask the user to provide the path instead, manually.
$message = "Path to \"mysqldump\" binary could not be detected!\n"
$message .= "Please, specify it inside the configuration file provided!"
throw new RuntimeException($message);
}
Now, you can use the above function for your purposes. And, provide a way for the user to provide the explicit path to mysqldump binary manually, if the above function throws an error. Should work for your use cases :)

Related

PHP Change default User on fopen

I'm having problems when trying to write a file with a different user rather than www-data.
I need to leave the file on a mapped unit that it's NOT mine so i cannot change permissions on that one. Insetead i have a username and a group, let's call them 'myself':'myself'
I'm using Symfony 5 Filesystem to move files around but i still use PHP's fopen to create those files. This is what i have so far.
private function moveAndDeleteFile($url_origin, $url_destiny)
{
$filesystem = new Filesystem();
$a = fopen($url_origin, 'wa+');
fwrite($a, 'Test');
fclose($a);
$filesystem->copy($url_origin, $url_destino, true);
if (!$filesystem->exists($url_destiny)) {
return false;
}
$filesystem->remove($url_origin);
return true;
}
This is just a test, so i'm creating a file (name included in $url_origin) and try to copy the file to $url_destiny and then remove the original (again, just a test).
The ting is that the file is always created by www-data and for my target directory i need to set the file owner to 'myself'.
Is there any way i can change the file owner with sudo?
$filesystem->chown($url_origin, 'myself', false);
This returns an error -> Failed to chown file "my/route/file.xml"
Same thing happens when trying
$filesystem->chgrp($url_origin, 'myself', false);
Then i tried to use the default PHP chown() function like this:
chown($url_origin, 'myself');
And get this other Error: Operation not permitted.
I guess that i need to specify somewhere the user's properties but i'm clueless right now. Any ideas on how to pass this through? i'm sure i'm missing something obvious.
Thanks-.
You cannot use "chown" because only "root" can use "chown".
The way is to set "chmod" or change configuration in your server with php-fpm running with myself user.
Regards

Can PHP resolve a Mac alias?

I am running MAMP and using PHP to read the names of all the files in a given folder. I would like to create an alias for certain images in another folder, and have PHP resolve these to their actual path. I know how to create symlinks that will work this way, but I would like to allow non-tech savvy web owners to use the Mac OS features that they are familiar with.
I have created a PHP script in the same folder as a alias that I have named test:
<?php
if (is_link ("test")) {
echo "is link";
} else {
echo "is not link";
}
?>
This echoes "is not link". I have tried using the fread() command on the link, but PHP seems to hang. It neither logs an error nor responds. I have tried opening the alias in a hex editor to see what it contains... but the hex editor opens what seems to be a huge file (if the alias is to a file), or opens the target folder (if the alias is to a folder).
Is there a way to help PHP to resolve the path in the alias? Is there an AppleScript function that I can call?
Chances are you're not referring to an actual symbolic link. If you're dealing with Finder Aliases, you can use the workaround found in the comments for the is_link docs.
Here are the contents of the comment (to avoid a link-only answer for posterity):
if( getFinderAlias( $someFile , $target ) ) {
echo $target;
}
else {
echo "File is not an alias";
}
function getFinderAlias( $filename , &$target ) {
$getAliasTarget = <<< HEREDOC
-- BEGIN APPLESCRIPT --
set checkFileStr to "{$filename}"
set checkFile to checkFileStr as POSIX file
try
tell application "Finder"
if original item of file checkFile exists then
set targetFile to (original item of file checkFile) as alias
set posTargetFile to POSIX path of targetFile as text
get posTargetFile
end if
end tell
end try
-- END APPLESCRIPT --
HEREDOC;
$runText = "osascript << EOS\n{$getAliasTarget}\nEOS\n";
$target = trim( shell_exec( $runText ) );
return ( $target == "" ? false : true );
}
Here's some explanation about symlinks vs. aliases. Really though, you should avoid using Apple's abstractions and just create a symlink:
ln -s /path/to/file /path/to/symlink

PHP lstat command doesn't distinguish shortcuts in windows

In windows, I open a dir, read the files, and for each file, run stat to determine the size, etc.
The problem is that when I run stat on a folder SHORTCUT, it comes back as a FOLDER, and I can't see anywhere in the mode bitmask that might indicate this. This has been true for all of the folder shortcuts in c:\Documents and Settings\myUserName\.
For these shortcuts, is_file returns false, is_dir returns true and is_link isn't supported in XP.
Here's an excerpt from my code (it has been trimmed down, so there may be bugs) :
if(($h=#opendir($root))!==false){
while (false !== ($file = readdir($h))){
if(!($file=="." || $file=="..")){
if( $stat = #lstat($root . $file) ){
$ary[0] = $file;
$ary[1] = $root;
$ary[2] = Date("m/d/y H:i:s", $stat['mtime']);
if($stat['mode'] & 040000){
$ary[3]="dir";
$ary[4]=0;
}else{
$ary[3] ="file";
$ary[4] = $stat['size'];
}
echo(json_encode($ary));
}
}
}
}
A workaround for this will be appreciated...
EDIT: Winterblood's solution almost worked
First off - my bad - it's a win7 machine.
Thanks Winterblood for the quick turnaround - this worked for several of the shortcuts, and the PHP manual says just that... However,
c:\users\myUserName\AppData\Local\Application Data
(and others) are still coming back as directories, while winSCP correctly sees them as shortcuts. As a matter of fact, the 'mode' is 040777, which is exactly the same as many real folders.
Any other suggestions?
PHP's stat() function "follows" shortcuts/symlinks, reporting details on the linked file/folder, not the actual link itself.
For getting stat details on the link itself use lstat().
More information in the PHP documentation on lstat.

mysqldump not working at all :(

I have this code :
function export()
{
$exp = system("mysqldump -uguku -pjustbe repadmin > back-up.sql");
if($exp) {echo 'ok';}
else { echo 'err';}
}
But it doesn't work, all it does, is to create the "back-up.sql" file, but it's blank.
And I get the "err" message.
Try using the full path to mysqldump...
$exp = system("/full/path/mysqldump -uroot -p repadmin > back-up.sql");
which mysqldump will tell you what the path should be.
Here you have a command that's not running properly. Best thing to do is to capture stderr (where error messages go) and see what the output is. Another thing to note is that environment variables will be different - such as the PATH (refered to by #Joshua_Martell above).
Use this command to capture stderr and see what error messages are being produced:
/usr/bin/mysqldump -uguku -pjustbe repadmin > back-up.sql 2> mysqldump.err
This also uses a full path to mysqldump (use the right path). You also probably should add a path to back-up.sql and, in this case, to mysqldump.err - don't assume that PHP will run in the directory you want as it could change.

Programmatically determine video file format?

Ok, I get the basics of video format - there are some container formats and then you have core video/audio formats. I would like to write a web based application that determines what video/audio codec a file is using.
How best can I programmatically determine a video codec? Would it be best to use a standard library via system calls and parse its output? (eg ffmpeg, transcode, etc?)
mplayer -identify will do the trick. Just calling ffmpeg on a file will also work--it will automatically print a set of info at the start about the input file regardless of what you're telling ffmpeg to actually do.
Of course, if you want to do it from your program without an exec call to an external program, you can just include the avcodec libraries and run its own identify routine directly.
While you could implement your own detection, it will surely be inferior to existing routines given the absolutely enormous number of formats that libav* supports. And it would be a rather silly case of reinventing the wheel.
Linux's "file" command can also do the trick, but the amount of data it prints out depends on the video format. For example, on AVI it gives all sorts of data about resolution, FOURCC, fps, etc, while for an MKV file it just says "Matroska data," telling you nothing about the internals, or even the video and audio formats used.
I have used FFMPEG in a perl script to achieve this.
$info = `ffmpeg -i $path$file 2>&1 /dev/null`;
#fields = split(/\n/, $info);
And just find out what items in #fields you need to extract.
You need to start further down the line. You need to know the container format and how it specifies the codec.
So I'd start with a program that identifies the container format (not just from the extension, go into the header and determine the real container).
Then figure out which containers your program will support, and put in the functions required to parse the meta data stored in the container, which will include the codecs.
-Adam
You really want a big database of binary identifying markers to look for near the start of the file. Luckily, your question is tagged "Linux", and such a dabase already exists there; file(1) will do the job for you.
I would recommend using ffprobe and force output format to json. It would be so much easier to parse. Simplest example:
$meta = json_decode(join(' ', `ffprobe -v quiet -print_format json -show_format -show_streams /path/to/file 2>&1`));
Be warned that in the case of corrupted file you will get null as result and warning depending on your error reporting settings. Complete example with proper error handling:
$file = '/path/to/file';
$cmd = 'ffprobe -v quiet -print_format json -show_format -show_streams ' . escapeshellarg($file).' 2>&1';
exec($cmd, $output, $code);
if ($code != 0) {
throw new ErrorException("ffprobe returned non-zero code", $code, $output);
}
$joinedOutput = join(' ', $output);
$parsedOutput = json_decode($joinedOutput);
if (null === $parsedOutput) {
throw new ErrorException("Unable to parse ffprobe output", $code, $output);
}
//here we can use $parsedOutput as simple stdClass
You can use mediainfo:
sudo apt-get install mediainfo
If you just want to get video/audio codec, you can do the following:
$videoCodec = `mediainfo --Inform="Video;%Format%" $filename`;
$audioCodec = `mediainfo --Inform="Audio;%Format%" $filename`;
In case you want to capture more info, you can parse XML output returned by mediainfo. Here is sample function:
function getCodecInfo($inputFile)
{
$cmdLine = 'mediainfo --Output=XML ' . escapeshellarg($inputFile);
exec($cmdLine, $output, $retcode);
if($retcode != 0)
return null;
try
{
$xml = new SimpleXMLElement(join("\n",$output));
$videoCodec = $xml->xpath('//track[#type="Video"]/Format');
$audioCodec = $xml->xpath('//track[#type="Audio"]/Format');
}
catch(Exception $e)
{
return null;
}
if(empty($videoCodec[0]) || empty($audioCodec[0]))
return null;
return array(
'videoCodec' => (string)$videoCodec[0],
'audioCodec' => (string)$audioCodec[0],
);
}

Categories