I have this code, for an Upload Form, that works successfully renaming and moving an uploaded file to the uploads/ folder. If an image isn’t uploaded, how can I put a default file where the file would have been if it were uploaded?
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"] != "" )
{
else if ($_FILES["file"]["size"] >= 100000)
{
echo ("<div class=\"errorMessage1\">Error1</div>");
}
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $thumbnail);
$_SESSION['thumbnail'] = $thumbnail;
$file_location = '<a href="http://www.........com/uploads/' . $thumbnail . '">' . $thumbnail . '</a>';
Any help will be appreciated.
Thanks for your reply.
Can you please give me an example of how that would work in the code I posted?
It’s a slow day at work.
if ($form_submitted == 'yes') {
$defaultFilePath = ''; // 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"] != "" )
{
$source = $defaultFilePath;
}
else if ($_FILES["file"]["size"] >= 100000)
{
echo ("<div class=\"errorMessage1\">Error1</div>");
$source = $defaultFilePath;
}
else
{
$source = $_FILES["file"]["tmp_name"];
}
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
if ($source === $defaultFilePath)
{
copy($source, $thumbnail);
}
else
{
move_uploaded_file($source, "uploads/" . $thumbnail);
}
$_SESSION['thumbnail'] = $thumbnail;
$file_location = '<a href="http://www.........com/uploads/' . $thumbnail . '">' . $thumbnail . '</a>';
That should do it.
Thanks much. I’ll give it a try
Thanks again for your help. However, I uploaded ‘test.png’ and set the path in the code, proceeded, but it wasn’t successful. I probably have something not quite right. Here’s my code:
if ($form_submitted == 'yes') {
$defaultFilePath = 'http://www.......com/uploads/test.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, $thumbnail);
}
else
{
move_uploaded_file($source, "uploads/" . $thumbnail);
}
$_SESSION['thumbnail'] = $thumbnail;
$file_location = '<a href="http://www.......com/uploads/' . $thumbnail . '">' . $thumbnail . '</a>';
}
Any additional help will be appreciated.
I did make a mistake - look at the arguments of copy and move_uploaded_file to find it. And while I didn’t do that on purpose, it will help you more in the long run if I let you figure out what I overlooked the first time on your own. If you’re still stuck in a couple days I’ll point it out.
Thanks for your reply.
I tried several things without success.
Any help with the mistake, will be appreciated.
system
Closed
March 19, 2015, 9:56pm
9
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.