HaLo2FrEeEk Posted August 16, 2007 Share Posted August 16, 2007 I made a topic about this before, but got one post helping, and nothing more, even though the post did not solve the problem. I need help uploading a file and sending it to a commandline windows executable. The program is called nconvert.exe, I will be uploading dds textures and converting them to bmp pictures, then immediately offering them back for download, not storing them on the server. The command to convert a file is this: nconvert -out bmp %1 %1 being the file passed to the executable. My question is this, can I use exec or passthru to run this program on the file uploaded, then pass the file back to the user, without storing it at all? Thanks to anyone who helps. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/ Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 I pretty sure no. In fact i'm like 99% sure no. A command line program is expecting a filename, not a datastream and certainly not an incomplete but possibly still uploading file. So, take the file the user gives you, save it, run the app, delete the file they gave you and return the new file. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-325491 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 17, 2007 Author Share Posted August 17, 2007 I couldn't pass the $_FILES['userfile']['tmp_name'] to the program? Or would I have to do something like this? $destdir = "./"; if(move_uploaded_file($_FILES['userfile']['tmp_name'], $destdir) { passthru("nconvert -out bmp $_FILES['userfile']['name']"); unlink($_FILES['userfile']['name']); } Would that work? Of course, I would have to implement something to check the error level of the upload file, $_FILES['userfile']['error'], but that would be outside this. Would this work? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-326350 Share on other sites More sharing options...
d22552000 Posted August 17, 2007 Share Posted August 17, 2007 ya that would work, you mind sharing your source? I am interested in your upload script as it is. I have not been able to make an upload script that actually could read teh file -,-. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-326352 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 17, 2007 Author Share Posted August 17, 2007 Sure, I use basically just this: <?php $destdir = "uploaded/" . basename($_FILES['userfile']['name']); switch($_POST['action']) { case "upload": switch ($_FILES['userfile']['error']) { case 0: if(move_uploaded_file($_FILES['userfile']['tmp_name'], $destdir)) { echo "File <b>".$_FILES['userfile']['name']."</b> uploaded successfully!<br /><br />\n"; echo "Filesize: ".$_FILES['userfile']['size']."<br />\n"; echo "MIME type: ".$_FILES['userfile']['type']."<br /><br />\n"; echo "URL to file: ".$_SERVER['HTTP_REFERER'].$destdir; } else { echo "File upload failed."; } break; case 1: echo "<p>The file is bigger than this PHP installation allows.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 2: echo "<p>The file is bigger than this form allows.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 3: echo "<p>Only part of the file was uploaded.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 4: echo "<p>No file was uploaded.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; } break; default: echo <<<EOF <form enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="26214400" /> <!-- Name of input element determines name in $_FILES array --> <input type="file" name="userfile" /> <input type="hidden" name="action" value="upload" /> <input type="submit" value="Upload" /> </form> EOF; break; } ?> It works for me and will upload the file to /uploaded when it's done, so make sure that directory is present, or change $destdir. Anyways, the script would work, I don't wanna screw up my server so I'm asking people here first. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-326480 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 18, 2007 Author Share Posted August 18, 2007 First off, bump. Second off, I'm using this code: <?php $title = "Upload Form"; include($_SERVER['DOCUMENT_ROOT']."/header.php"); $putfiles = "./" . basename($_FILES['userfile']['name']); switch($_POST['action']) { case "upload": switch ($_FILES['userfile']['error']) { case 0: $ext = strtolower(substr($_FILES['userfile']['name'], strrpos($_FILES['userfile']['name'], '.')+1, strlen($_FILES['userfile']['name']))); if($ext != "dds") { echo "You must upload only DDS textures<br /><br />"; die(include($_SERVER['DOCUMENT_ROOT'].'/footer.php')); } if(move_uploaded_file($_FILES['userfile']['tmp_name'], $putfiles)) { echo "Success moving file<br /><br />\n\n"; $f = $_FILES['userfile']['name']; echo "Success naming variable<br /><br />\n\n"; if(exec("nconvert.exe -out bmp $f")) { echo "Success converting file<br /><br />\n\n"; } else { echo "Failed converting file<br /><br />"; } //unlink($f); //echo "Success deleting file<br /><br />\n\n"; } else { echo "File upload failed."; } break; case 1: echo "<p>The file is bigger than this PHP installation allows.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 2: echo "<p>The file is bigger than this form allows.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 3: echo "<p>Only part of the file was uploaded.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; case 4: echo "<p>No file was uploaded.</p>\n"; echo "<p><a href=\"{$_SERVER['PHP_SELF']}\">Please try again.</a></p>"; break; } break; default: echo <<<EOF <form enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="26214400" /> <!-- Name of input element determines name in $_FILES array --> <input type="file" name="userfile" /> <input type="hidden" name="action" value="upload" /> <input type="submit" value="Upload" /> </form> EOF; break; } include($_SERVER['DOCUMENT_ROOT']."/footer.php"); ?> Which will tell me if I'm really converting the file or not, and I get this output: Success moving file Success naming variable Failed converting file Please, someone help. I used to get such good help around here and now everyone ignores my posts. I need some help, guys. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-327345 Share on other sites More sharing options...
markjoe Posted August 18, 2007 Share Posted August 18, 2007 What does the nconvert.exe program return? As in when you use it in a command window, what does it print to the screen when it is finished converting? If it does not return something that translates to true, your script will always claim it didn't work even if it did. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-327377 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 20, 2007 Author Share Posted August 20, 2007 Oh, well it outputs this: ** NCONVERT v4.79 (c) 1991-2007 Pierre-E Gougelet (May 16 2007/10:27:55) ** Version for Windows NT/9x/2000/Xp/Vista (All rights reserved) ** This is a freeware software (for non commercial use) Conversion of {sourcefile} into {outputfile} OK I replaced the source and output files, becuase the filenames didn't matter, but this is the output. It still doesn't do anything though, I ran the uploader without the exec and it uploaded the dds file just fine, I then deleted the dds and ran it with the exec and I'm assuming that it uploaded it, then deleted it, completely skipping the exec becuase there was no bmp on the server. Maybe I'll try it without deleting it, just to see if maybe it's deleting the bmp too, though I don't know how it would be. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328685 Share on other sites More sharing options...
itsmeArry Posted August 20, 2007 Share Posted August 20, 2007 are you using window or linux..... if you are using windows then you need to change one thing... go to control panel, open administrative tools, and then go to Services.... find the service.... "server" click on it and then click on Properties. then click on "Log On" tab... there check the check box for "Allow this service to interact with desktop." click on apply changes and then restart the server. "I too ran into this kind of problem once. I was supposed to upload audio file and convert the into another format using a software. and then save them to another location. The above procedure helped me in solving the problem you are facing right now." Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328703 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 20, 2007 Author Share Posted August 20, 2007 Well, I'm not hosting this myself, I'm using dreamhost, I think they're linux, actually I'm almost positive they are. Anyways, I have no access to server configurations, I just need to be able to run this nconvert program, will it not work with linux becuase it's a windows executable? Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328709 Share on other sites More sharing options...
itsmeArry Posted August 20, 2007 Share Posted August 20, 2007 if you can upload a file through ftp... then you can find out whish os the server is using. write this code in a test.php file and upload it to the server then run this file you will find almost all the environment variables. and find out which server you are using. <?php echo "<pre>"; print_r($_ENV); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328713 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 20, 2007 Author Share Posted August 20, 2007 Apache/1.3.37 (Unix) mod_throttle/3.1.2 DAV/1.0.3 mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a PHP/4.4.7 mod_ssl/2.8.22 OpenSSL/0.9.7e Apache, I guess. Somewhere in my mind I thought I remembered seeing apache, I must have connected it with something else. I'm using apache. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328718 Share on other sites More sharing options...
itsmeArry Posted August 20, 2007 Share Posted August 20, 2007 there will be an index [OS] what dose that say..... Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328724 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 20, 2007 Author Share Posted August 20, 2007 Nope, not there, sorry, I don't have that index. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328733 Share on other sites More sharing options...
itsmeArry Posted August 20, 2007 Share Posted August 20, 2007 ok lets c if we can narrow it down.. is this .exe you are trying to execute on the server I think you need to pass the physical path of the file you need to convert. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328739 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 20, 2007 Author Share Posted August 20, 2007 It's in the same directory as the upload file. Would I need to pass the absolute path to the file, like: /home/halo2freeek/claninfectionist.com/misc/testing/nconvert.exe ? Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328747 Share on other sites More sharing options...
trq Posted August 20, 2007 Share Posted August 20, 2007 Chances are pretty high that your server is Linux, not windows. And a windows .exe will not run on Linux. You'll need to find a Linux equivelent, then find out if it is installed on your server. Quote Link to comment https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/#findComment-328813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.