Jump to content

Invalid file


stuffradio

Recommended Posts

I'm trying this Image uploader:

 

<?php
$target_path = $_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/";
$topx = $_POST['topxstart'];
$topy = $_POST['topystart'];
$target_path = $target_path . basename( $_FILES['file']['name']); 
$target_pathoriginal = $_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . basename( $_FILES['file']['name'] . 'original');
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  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 />";
echo "Image: <img src=imagedisplay.php?text=" . $_POST[titletext] ."&file=".$_FILES["file"]["name"]. "&x=" . $topx . "&y=" . $topy ." border=0 />";
    if (file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    elseif (file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"]. 'original'))
      {
  echo $_FILES["file"]["name"] . " already exists. ";
} else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $target_path);
        move_uploaded_file($_FILES["file"]["tmp_name"],
      $target_pathoriginal);
      echo "Stored in: " . file_exists($_SERVER['DOCUMENT_ROOT']."wp-2.7/wp-content/uploads/" . $_FILES["file"]["name"]);
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  ?>

 

Some JPEG images I upload make the code output Invalid file. I'm not sure why it's invalid since the type of image is JPEG, 37 KB, 400 x 491 dimention. Any ideas why it would be invalid?

 

Also, do you know of any class that would help with writing titles and captions on images and save the image to a path?

Link to comment
Share on other sites

That code, which you may in fact have gotten from a trusted source, is crap. It tests for upload errors after it has already attempted to test the ['type'] and ['size']. The type/size won't exist for many of the possible upload errors. It also lumps together the test for type/size, so you will never know which one of these things was the problem, and it does not tell you in the error messages why it failed to work or what the actual values were that caused it to fail.

 

For debugging to find out what you are actually getting, add the following code -

 

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";

 

And the code needs to be rewritten to test for upload errors first, then test the type, then test the size (all separately) and output a meaningful user message for upload errors, incorrect types, and invalid size (output the upload error, type and size of the uploaded file in the error messages so you will know what was wrong.)

 

 

Link to comment
Share on other sites

I think I copied it from W3schools and edited it a bit to do what I need, where is the best and cleanest uploader do you think? Also, is it possible to do the image(insert file format here) function on the same page?

 

Right now I need to use two php files, one for the image upload and one to display the image and write the text.

Link to comment
Share on other sites

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.