Page 1 of 1

problem with php upload script

Posted: Mon Jan 25, 2010 5:21 am
by whistler
I'm having a problem with a bit of simple php for uploading documents to an intranet server. The tricky part (for me at least) is that the bulk of the web app is in asp, but my coding skills and my level of sleep deprivation make me not up to the task of comprehending any of the examples of asp file upload code that I've been able to find.

So what I'm trying to do is this: I've got an integer that's getting passed through a url to my upload.php file which is just a form with a browse button and a submit button like so:

Code: Select all

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<input type="hidden" name="number" id="number" value="<?php $cnumber = $_GET['cnumber']; echo $cnumber?>">
<br />
<input type="submit" name="submit" value="Submit" />
</form>
 
</body>
</html>
you can see my aborted attempt at passing the integer through to the code file through a hidden form field. I suspect this is a potential, if not elegant, way to do it but I can't wrap my brain around this right now and I need to get this done in the next 5 hours or so if possible.

This next bit of code is the part that actually writes the file to a folder on the server. Again, it's simple enough. I need the integer from the web app to use for the file name because the application is the front end for a database and each file is in reference to the DB.

Code: Select all

<?php
if ($_FILES["file"]["size"] < 2000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["number"]."1".".doc");
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
You can see here, in the second to last else statement, where I am trying to construct a file name from the integer plus the number 1 and the extension ".doc."

Yes, I realize that forcing the extension on a file without forcing the file type could quite easily create a problem, but I searched high and lo and couldn't find the correct file type syntax for anything besides image files. So if some wonderful soul helps me out and knows how to specify only .doc files can be uploaded, that'd be neat too.

hoping for a solution,
Tom

Re: problem with php upload script

Posted: Mon Jan 25, 2010 9:29 am
by AbraCadaver
I'm not really sure of your question except for the doc type, but looking at your code it doesn't appear that it will work correctly.

Code: Select all

    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
// you are checking for the posted filename, not your new one with the number
    $newname = $_POST["number"] . ".doc";
    if (file_exists("upload/" . $newname))
    //if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo "file already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $newname);
      echo "Stored in: " . "upload/" . $newname;

Re: problem with php upload script

Posted: Mon Jan 25, 2010 1:03 pm
by whistler
Yeah, alright. Maybe I wasn't super clear. The problem I'm having is less with the actual upload code, and the concatenation to create the new file name, it is with getting this variable from the starting asp page, to the upload form (through the url, ie http://xxx.xxx.xxx.xxx/upload.asp?number=522) and then through to the upload_file.php page (the upload code page) to be able to use it to create the new file name. The new file name should be the form of $variable-1.doc (I need to have two documents per variable, thus this whole mess.) The other document will be $variable-2.doc.

Is that any more enlightening?

Re: problem with php upload script

Posted: Mon Jan 25, 2010 1:47 pm
by whistler
I got it working. If someone could still give me the exact syntax to only allow .doc files that would be awesome though. I'm sure it's probably it's probably sitting in some thread somewhere, but I can't seem to find it no matter which way I search.

I have this code to allow specific types of images:

Code: Select all

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))

So I just need to know what to use instead of image/jpeg. I tried file/doc and text/doc and neither seemed to work, but I was somewhat delirious at that point so maybe I did it wrong.

if anyone is interested in the (probably extremely bad code) that I ended up getting to work:
form page:

Code: Select all

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<input type="hidden" name="number" id="number" value="<?php print $_REQUEST['cnumber'] ?>">
<br />
<input type="submit" name="submit" value="Submit" />
</form>
 
</body>
</html>
upload code:

Code: Select all

<?php
$number = $_REQUEST['number'];
if ($_FILES["file"]["size"] < 2000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
   
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $number."-1".".doc");
      echo "Stored in: " . "upload/" . $number."-1".".doc"."<br />";
      echo "upload successful";
    }
  }
else
  {
  echo "Invalid file";
  }
?>