Jump to content

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

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.