Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/65189-exec-a-commandline-program/
Share on other sites

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.

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.

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.

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.

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.

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.

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."

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?

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);
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.