Jump to content

File Upload


Spudster

Recommended Posts

Hi,

The question I have is regarding php file upload.  What i need is a way to check and see if a filename is taken before it is uploaded.  PHP does this on its own, but you get some generic php fatal error.  I want to make it so I can make something comes up, just like a warning that the filename is taken.  Here is the script for that part:

<?php

  if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:  echo 'File exceeded upload_max_filesize';  break;
      case 2:  echo 'File exceeded max_file_size';  break;
      case 3:  echo 'File only partially uploaded';  break;
      case 4:  echo 'No file uploaded';  break;
    }
    exit;
  }

  // Does the file have the right MIME type?
  if ($_FILES['userfile']['type'] != 'text/plain')
  if ($_FILES['userfile']['type'] != 'video/quicktime')
  {
    echo 'Problem: file is not a text document/Quicktime video';
    exit;
  }

  // put the file where we'd like it
  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name']))
  {
    if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
    {
        echo 'Problem: Could not move file to destination directory';
        exit;
    }
  }
  else
  {
    echo 'Problem: Possible file upload attack. Filename: ';
    echo $_FILES['userfile']['name'];
    exit;
  }
  echo 'File uploaded successfully<br><br>';
?>


Any help would be great.  Thanks
Link to comment
https://forums.phpfreaks.com/topic/28940-file-upload/
Share on other sites

[quote]PHP does this on its own, but you get some generic php fatal error.[/quote]
which line of code gives the error.?

TIP..
put the @ symbol in front of the offending function call to suppress that fatal 'generic' error

example
[code=php:0]
$moveFile = @!move_uploaded_file($thisfile, $here);
if(!$moveFile)
  show error
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28940-file-upload/#findComment-132534
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.