Jump to content

php upload problem


psychohagis

Recommended Posts

I have the following scipt to upload avatars which can either be .gifs or .jpegs - and i have a process for checking that but every time i run this script it complains that the file is not a gif or jpeg, even when it is

[code]
<?php
session_start();

if (!isset($_SESSION['userid']) or $_SESSION['userid'] =='')
{
exit('You must be signed in to do that');
}


$username= $_SESSION['username'];


if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])
    or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) {
    //handle the file...
} else {
exit 'You must upload a .gif or .jpeg file';
}


if (eregi('^image/p?jpeg(;.*)?$',
    $_FILES['upload']['type'])) {
    $extension = '.jpg';
} else {
    $extension = '.gif';
}


$filename = 'avatars/' . $username . $extension;

if (is_uploaded_file($_FILES['upload']['tmp_name']) and
    copy($_FILES['upload']['tmp_name'], $filename)) {
  echo 'Avatar uploaded succesfully';
} else {
  echo 'Avatar could not be uploaded';
}

?>
[/code]
Link to comment
Share on other sites

Here's what I use:

[code]<?php
session_start();

if($_SESSION['logged_in'] == true) {

$file_upload = "true";

if ($imgfile_size > 125999)
{
$msg="Your uploaded file must be less than 125kb.<BR>";
$file_upload="false";
}

if (!($imgfile_type=="image/jpeg" || $imgfile_type=="image/gif"))
{
$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}




if($file_upload=="true") {

$folder="$_SESSION[user_name]";
if(!is_dir("thumbs/".$folder)) {
mkdir("thumbs/".$folder,0777);
}

$uploaddir = 'thumbs/'. $folder ."/";
$uploadfile = $uploaddir . basename($_FILES['imgfile']['name']);

if(!file_exists($uploaddir.".htaccess")) {
copy("thumbs/.htaccess", $uploaddir .".htaccess");
}

echo '<pre>';
if (move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile)) {
  echo "File upload was successful.<br><br><a href=\"$uploadfile\">View: $uploadfile</a>\n";
} else {
  echo "File upload failed.\n";
}

}

if($file_upload=="false") {
echo" <h2>Error:</h2><br><br>
$msg";
}
}

else {
echo" Not Logged In...";
}
?>


[/code]

Although mine is set up to first check if the user is logged in and then create a new folder for that user with 777 permissions for storing their pics.  Inside that folder I later add a .htaccess file limiting what can be uploaded to that folder and executed from that folder..you may want to just cut that out of there
Link to comment
Share on other sites

Change this:
[code]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])
    or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) {
    //handle the file...
} else {
exit 'You must upload a .gif or .jpeg file';
}[/code]
To:
[code]
if (($_FILES["upload"]["type"] == "image/jpg")
|| ($_FILES["upload"]["type"] == "image/gif")
|| ($_FILES["upload"]["type"] == "image/jpeg")
|| ($_FILES["upload"]["type"] == "image/pjpeg"))
{
//handle the file...
}
else
{
echo 'You must upload a .gif or .jpeg file';
exit;
}
[/code]
Link to comment
Share on other sites

umm no i dont see anything. could it be something wrong with my form:

[code]<form action=upload.php method=post enctype=multipart/form-data>
<input type=hidden name=MAX_FILE_SIZE value=500000 />
<p><label>Select file to upload:
<input type=file name=uploadedfile /></label></p>
<p><input type=submit name=submit value=submit /></p>
</form>[/code]
Link to comment
Share on other sites

You need to change all instances of [code=php:0]$_FILES['upload'][/code] to [code=php:0]$_FILES['uploadedfile'][/code] as that's the field name in your form.

So this: [code=php:0]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']){[/code]

Would look like this: [code=php:0]if (eregi('^image/p?jpeg(;.*)?$', $_FILES['uploadedfile']['type']){[/code]

Regards
Huggie
Link to comment
Share on other sites

Ok im now running it as follows:

[code]<?php
session_start();

if (!isset($_SESSION['userid']) or $_SESSION['userid'] =='')
{
exit('You must be signed in to do that');
}


$username= $_SESSION['username'];






if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])
    or eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])) {
    //handle the file...
} else {
echo "You must upload a .gif or .jpeg file";
exit;
}


if (eregi('^image/p?jpeg(;.*)?$',
    $_FILES['upload']['type'])) {
    $extension = '.jpg';
} else {
    $extension = '.gif';
}


$filename = 'avatars/' . $username . $extension;

if (is_uploaded_file($_FILES['upload']['tmp_name']) and
    copy($_FILES['upload']['tmp_name'], $filename)) {
  echo 'Avatar uploaded succesfully';
} else {
  echo 'Avatar could not be uploaded';
}



?>
[/code]

So it now runs throught the script and tells me the file is uploaded succesfully, but i can see it in the relevant folder on my server
Link to comment
Share on other sites

Not quite how it works. A file gets uploaded to the temp folder, and you need to move it.
Read this tutorial:
http://www.tizag.com/phpT/fileupload.php

(There's not a good one here on PHP Freaks I can see, if there is someone correct me please!)
Link to comment
Share on other sites

My advice would be to try this code.  I've added a few comments too.

[code]<?php
// Start the session
session_start();

// Check that the user is authenticated
if (!isset($_SESSION['userid']) or $_SESSION['userid'] ==''){
  exit('You must be signed in to do that');
}
$username = $_SESSION['username'];

// While trouble shooting, dump the variable to check values are OK
echo "<pre>";
print_r($_FILES);
echo "</pre>";

// Make sure the file that's uploaded is either .gif or .jpg
if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type']) OR eregi('^image/gif(;.*)?$', $_FILES['upload']['type'])){

  // Decide what the extension is going to be
  if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])) {
      $extension = '.jpg';
  }
  else {
      $extension = '.gif';
  }

  // Compose the new file name
  $filename = 'avatars/' . $username . $extension;

  // Check the file's been uploaded
  if (is_uploaded_file($_FILES['upload']['tmp_name'])){

      // Copy it to its new location
      if (copy($_FILES['upload']['tmp_name'], $filename)){
        echo 'Avatar uploaded succesfully';
      }
      else {
        echo 'Unable to copy avatar to its new location';
      }
  }
  else {
      echo 'Avatar could not be uploaded';
  }
}
else {
  echo "You must upload a .gif or .jpeg file";
  exit;
}
?>[/code]
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.