I did make a mistake - look at the arguments of copy and move_uploaded_file to find it

Can you help me find the mistake? I received help with my request for help in a posting:
“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?”.

I’m no longer asking for help with a “default file”, but am asking for help with the reply/help where I was given coding assistance that didn’t work successfully and was told “I did make a mistake - look at the arguments of copy and move_uploaded_file to find it” See: How to add a default file - #7 by Michael_Morris
for more info.
Anyway, I tried several things to correct the mistake, without success. Can someone help me, please, correct the mistake in this 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.

This was my mistake: when assembling the $thumbnail string I copied your code and didn’t notice you where prepending ‘uploads’ in your move_uploaded_file call. This prepending must also be applied to the copy call.

Or you need to apply the prepending when the rest of the thumbnail string is applied.

Like most experienced programmers I assumed that all operations that were to be applied to thumbnail were being done before the function call. If I paid more attention I would have caught it.

Thank you again for your reply. I’ve tried the code below without success. Any additional help will be appreciated.

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, "uploads/" . $thumbnail);
 }
 else
 {
 move_uploaded_file($source, "uploads/" . $thumbnail);
}
$_SESSION['thumbnail'] = $thumbnail;
 $file_location = '<a href="http://www.......com/uploads/' . $thumbnail . '">' . $thumbnail . '</a>';
}

.if (!in_array($extension,$allowedExts) || empty($_FILES[“file”][“name”]))
{
EDIT: OK, I see why you had it like you have it. Just didn’t look right at first. Moderator can remove this post.

Your default file path cannot be a web location, it should be a local system path.

1 Like

Thanks for your reply. I tried this without success:
$defaultFilePath = ‘…/uploads/test.png’; // set this.

If you could provide an example of what you mean by “a local system path”, it would be greatly appreciated.

You need one more ELSE or the variable $source is not defined when it passes the extension and size conditions. Something like this.

    if (!in_array($extension,$allowedExts) && $_FILES["file"]["name"] != "" ){
        echo "<div id=\"errorMessage\">Error: Invalid File Name </div>";
        $source = $defaultFilePath;
    }elseif ($_FILES["file"]["size"] >= 100000){
        echo "<div class=\"errorMessage1\">Error: Image File Size Exceeds Limit </div>"; 
        $source = $defaultFilePath;
    }else{
        $source = $_FILES["file"]["tmp_name"];
    }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.