problem with php upload script
Posted: Mon Jan 25, 2010 5:21 am
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:
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.
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
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>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";
}
?>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