| | |
PHP upload issues - uploading not working on some machines
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi all
I have some functionality that allows the upload of files such as MS word docs, zips etc. It works fine on my pc and another test machine but doesn't seem to work on two other machines I've tested. All machines have Windows XP installed. The first machine that is having problems only has issues uploading PDF's. The second machine having problems can't upload anything at all.
Please could someone have a look at my code below and tell me if there's anything there that could be causing these problems. I'm pretty sure I have the MIME types right. I'm really puzzled as to what is causing this.
P.S. My code isn't the best in the world. Note that this upload script is not being used publicly on the website so security issues aren't top priority.
I have some functionality that allows the upload of files such as MS word docs, zips etc. It works fine on my pc and another test machine but doesn't seem to work on two other machines I've tested. All machines have Windows XP installed. The first machine that is having problems only has issues uploading PDF's. The second machine having problems can't upload anything at all.
Please could someone have a look at my code below and tell me if there's anything there that could be causing these problems. I'm pretty sure I have the MIME types right. I'm really puzzled as to what is causing this.
P.S. My code isn't the best in the world. Note that this upload script is not being used publicly on the website so security issues aren't top priority.
php Syntax (Toggle Plain Text)
class DocumentUpload { //Variables values gained from the __construct() method public $inputName; public $original; public $timeOriginal; public $tempName; public $docType; public $folder; public $dbFolder; public $page; public $pathAndFile; public $pathTimeFile; public $dbFile; public $dbTimeFile; //Variables used in the upload() method public $thumb; public $success; public $result; public $mailSubject = ""; //Variable used to see which path and filename must be added to the database public $timedFile; //Array of acceptable MIME types public $permitted = array('application/pdf', 'application/zip', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel'); //variables set assuming the file is unacceptable public $sizeOK = false; public $typeOK = false; public function __construct($inputName, $folder, $dbFolder, $page) { $this->inputName = $inputName; $this->original = str_replace(array(" ", 'PDF'), array("_", 'pdf'), $_FILES[''.$inputName.'']['name']); $this->timeOriginal = time().$this->original; $this->tempName = $_FILES[''.$inputName.'']['tmp_name']; $this->docType = $_FILES[''.$inputName.'']['type']; $this->folder = $folder; $this->dbFolder = $dbFolder; $this->pathAndFile = $folder.$this->original; $this->pathTimeFile = $folder.time().$this->original; $this->dbFile = $dbFolder.$this->original; $this->dbTimeFile = $dbFolder.time().$this->original; $this->page = $page; } //Upload an image to the server public function upload($chmod) { //check that the file is of a permitted MIME type foreach($this->permitted as $type) { if($type == $this->docType) { $this->typeOK = true; break; } } //Check if the file type has been verified as valid if($this->typeOK) { switch($_FILES[''.$this->inputName.'']['error']) { case 0: // Make sure file of the same name does not exist if (!file_exists($this->pathAndFile)) { $this->success = move_uploaded_file($this->tempName, $this->pathAndFile); chmod($this->pathAndFile, $chmod); //Variable used to see which path and filename must be added to the database $this->timedFile = false; } else { $this->success = move_uploaded_file($this->tempName, $this->pathTimeFile); chmod($this->pathTimeFile, $chmod); //Variable used to see which path and filename must be added to the database $this->timedFile = true; } //If the file was uploaded if ($this->success) { $this->result = "Document added successfully."; } else { $this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>."; } break; case 3: $this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>."; default: $this->result = "System error uploading file. Contact the <a class=\"clrBrown\" href=\"mailto:test@website.com".$this->mailSubject."\">webmaster</a>"; } } else if($_FILES[''.$this->inputName.'']['error'] == 4) { $this->result = 'No file selected'; } else { $this->result = "File cannot be uploaded. Acceptable file types: ppt, xls, doc, pdf, zip. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>."; } } }
This user has a spatula. We don't know why, but we are afraid.
Hey.
Looking over that, I see nothing essentially wrong with it.
I assume you have ruled out the usual file permissions issues and upload size limitations?
When the code is not working, does it give you any sort of error messages?
(Are error messages even turned on in the server?)
One the server that only blocked PDF files, did you try printing the mime type that you were actually getting?
Also, keep in mind that the type that is sent with the file is sent by the browser, so it is not 100% reliable or consistent.
(Different browsers may use different mime types for the same file type)
P.S.
The surrounding strings in all your array element names are not needed. Serve no purpose.
Looking over that, I see nothing essentially wrong with it.
I assume you have ruled out the usual file permissions issues and upload size limitations?
When the code is not working, does it give you any sort of error messages?
(Are error messages even turned on in the server?)
One the server that only blocked PDF files, did you try printing the mime type that you were actually getting?
Also, keep in mind that the type that is sent with the file is sent by the browser, so it is not 100% reliable or consistent.
(Different browsers may use different mime types for the same file type)
P.S.
$array[''.$name.''] should be $array[$name] .The surrounding strings in all your array element names are not needed. Serve no purpose.
Last edited by Atli; Oct 5th, 2009 at 8:09 am.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
•
•
Hey.
Looking over that, I see nothing essentially wrong with it.
I assume you have ruled out the usual file permissions issues and upload size limitations?
When the code is not working, does it give you any sort of error messages?
(Are error messages even turned on in the server?)
One the server that only blocked PDF files, did you try printing the mime type that you were actually getting?
Also, keep in mind that the type that is sent with the file is sent by the browser, so it is not 100% reliable or consistent.
(Different browsers may use different mime types for the same file type)
P.S.
$array[''.$name.'']should be$array[$name].
The surrounding strings in all your array element names are not needed. Serve no purpose.
Thanks for your tip in your P.S.
Last edited by Venom Rush; Oct 5th, 2009 at 8:15 am.
This user has a spatula. We don't know why, but we are afraid.
You can try the FileInfo module. It looks for certain byte-sequences within files to guess their mime type.
For images, you can use the getimagesize function. It returns the actual type of the image based on the data, rather then any outside info.
For PDF files, if you do not use FileInfo, you can read the first few bits of the file and check if they are really valid headers for a PDF document. All valid PDF documents should start with something like
It's not exactly bullet-proof, but it should at least filter out files that are just renamed to ".pdf".
For images, you can use the getimagesize function. It returns the actual type of the image based on the data, rather then any outside info.
For PDF files, if you do not use FileInfo, you can read the first few bits of the file and check if they are really valid headers for a PDF document. All valid PDF documents should start with something like
%PDF , so you could do: php Syntax (Toggle Plain Text)
<?php function validate_pdf($path) { $fh = fopen($path, "rb"); if($fh) { $header = fread($fh, 4); if($header == "%PDF") { return true; } else { return false; } } else { return false; } } ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
![]() |
Similar Threads
- Upload file via PHP upload form. (VB.NET)
- How we can upload a text file or word file using php Code??? (PHP)
- c# code is crashing when using threading (C#)
- php upload mysql (PHP)
- Uploading php files in ftp (PHP)
- upload (PHP)
- file upload issues (PHP)
- upload a doc file in php (PHP)
- Same application not working on all machines (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: two databases issue
- Next Thread: HELP Cron job PHP error filling log file
| Thread Tools | Search this Thread |
apache api array basic beginner binary broken cakephp checkbox class cms code computing confirm cron curl customizableitems database date delete display dynamic echo email error external file files filter folder form forms forum function functions gc_maxlifetime google headmethod host href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction memmory memory menu mlm multiple mysql navigation neutrality oop parsing paypal pdf php problem query question radio random recursion remote root script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validator variable video web xml youtube





