Jump to content

How to add a default file?


Chrisj

Recommended Posts

I have this code, for an Upload Form, that works successfully renaming and moving an uploaded file to the upload/ folder. 
If an image isn't uploaded, what code can I add to put a default file where the file would have been if it were uploaded? This code is what's working currently:
 
if ($form_submitted == 'yes') {
$allowedExts = array("gif", "jpeg", "jpg", "txt", "rtf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );
if (!in_array($extension,$allowedExts) && $_FILES["file"]["name"] != "" )
{
echo ("<div id=\"errorMessage\"> >> Error: Invalid File Name </div>");
}
else if ($_FILES["file"]["size"] >= 100000)
{
echo ("<div class=\"errorMessage1\"> >> Error: Image File Size Exceeds Limit </div>");
}
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
$_SESSION['thumbnail'] = $thumbnail;
$file_location = '<a href="http://www....com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';
}

This is my attempt to "put a default file where the file would have been if it were uploaded", but my additions didn't suceed:

if ($form_submitted == 'yes') {
  $defaultFilePath = '../upload/Default.png'; // set this.
  $allowedExts = array("gif", "jpeg", "jpg", "txt", "rtf", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = strtolower( end($temp) );
  if (!in_array($extension,$allowedExts) && $_FILES["file"]["name"] != "" )
    {
    echo ("<div id=\"errorMessage\"> >> Error: Invalid File Name </div>");
    $source = $defaultFilePath;
  }
  else if ($_FILES["file"]["size"] >= 100000)
  {
  echo ("<div class=\"errorMessage1\"> >> Error: Image File Size Exceeds Limit </div>");
    $source = $_FILES["file"]["tmp_name"];
  }
  $length = 20;
  $randomString = (time());
  $thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
  if ($source === $defaultFilePath)
  {
  copy($source, "upload/" . $thumbnail);
  }
  else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
  }
  $_SESSION['thumbnail'] = $thumbnail;
  $file_location = '<a href="http://www...com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';
}
Any help with getting this to work correctly will be appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/294009-how-to-add-a-default-file/
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.