Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Give better error messages in the example upload.php #629

Closed
wants to merge 2 commits into from

3 participants

@Velmont

Many people just need a quick uploader, and use the embedded example code. I actually thought the code was wrong, which it wasn't, but found my real error after actually printing out the error code that PHP provides.

This patch exposes that error code (along with a helpful message).

@mehran20

Sorry but:

-if (strpos($contentType, "multipart") !== false) {
+if (!strstr($contentType, "multipart")) {

didn't fix the problem for me! The uploaded file is 0 bytes while before this no file was created in the server. I'm using Chromium Version 20.0.1132.47 Ubuntu 12.04 (144678).

@Velmont

@mehran20 Hmm, you're in fact totally correct.

It's a totally different problem (on my side, and potentially at yours too):

upload_max_filesize is too small: http://no.php.net/manual/en/ini.core.php#ini.upload-max-filesize

@mehran20

No that's not it. I've set the upload_max_filesize to 20M and the file I'm trying to upload is 5M but the file created on server is 0 byte.

@Velmont

Okay, you have prolly a different problem then. But try this patch, it will might give you a better error message.

@jayarjo
Owner

@mehran20 were you able to upload the file the old way after you enlarged upload_max_filesize? Check my response in #589.

@Velmont in general upload.php is just an example. You can enhance it in any way you find appropriate. However we do not plan to expand it any further. Opposite in fact - I'm looking to cut it down :)

@jayarjo jayarjo closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 7 deletions.
  1. +17 −7 examples/upload.php
View
24 examples/upload.php
@@ -56,7 +56,7 @@
if (!file_exists($targetDir))
@mkdir($targetDir);
-// Remove old temp files
+// Remove old temp files
if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) {
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
@@ -70,7 +70,7 @@
closedir($dir);
} else
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
-
+
// Look for the content type header
if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
@@ -80,7 +80,7 @@
$contentType = $_SERVER["CONTENT_TYPE"];
// Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
-if (strpos($contentType, "multipart") !== false) {
+if (strstr($contentType, "multipart")) {
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
@@ -98,8 +98,18 @@
@unlink($_FILES['file']['tmp_name']);
} else
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
- } else
- die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
+ } else {
+ $error_msg = "Unknown error (possibly POST_MAX_SIZE is smaller than MAX_FILE_SIZE?)";
+ if (isset($_FILES['file']['error']) && $_FILES['file']['error'] != 0) {
+ $errors = array(1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
+ 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
+ 3 => "The uploaded file was only partially uploaded",
+ 4 => "No file was uploaded",
+ 6 => "Missing a temporary folder");
+ $error_msg = "Error code {$_FILES['file']['error']} ({$errors[$_FILES['file']['error']]})";
+ }
+ die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file: '.$error_msg.'."}, "id" : "id"}');
+ }
} else {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
@@ -121,7 +131,7 @@
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
- // Strip the temp .part suffix off
+ // Strip the temp .part suffix off
rename("{$filePath}.part", $filePath);
}
@@ -129,4 +139,4 @@
// Return JSON-RPC response
die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
-?>
+?>
Something went wrong with that request. Please try again.