Jump to content

Recommended Posts

adding photo,Array ( [name] => profilepic.jpeg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpB923.tmp [error] => 0 [size] => 152127 )

Hi,

We have been using this code for ages, but suddenly for some reason we cannot upload an image using the code below.

When we try to Print_r the $profilephoto variable, I get this error.

The filename is correct, but what is that [error]??  And how do I resolve it?

Oddly it does upload it locally, but LIVE, it won't 

This is the code.

if (isset($updatephoto))
{
echo "adding photo,";
print_r($profilephoto);
 define ("MAX_SIZE","5000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    $image =$_FILES["profilephoto"]["name"];
    $uploadedfile = $_FILES['profilephoto']['tmp_name'];

    if ($image) 
    {

        $filename = stripslashes($_FILES['profilephoto']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);


 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {
            echo "Unknown Extension..!";
        }
        else
        {

 $size=filesize($_FILES['profilephoto']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
    echo "File Size Excedeed..!!";
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['profilephoto']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['profilephoto']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
echo $scr;
}

list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$pic=($_FILES['profilephoto']['name']);
$random = (rand()%99999999);
$newname="$random"."$pic";
$filename = "images/profiles/". $newname;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}}
}
$query = ("UPDATE users SET profilephoto =:newname WHERE id =:userid");
$result = $pdo->prepare($query);
$result->execute(array(':userid' => $userid, ':newname' => $newname));
echo "<script>
          window.location.replace('/profile/')
          </script>";}

 

Link to comment
https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/
Share on other sites

31 minutes ago, simona6 said:

[error] => 0

if you are asking about the value zero in the ['error'] element, that means the upload was successful, which you would know by making use of the php.net documentation on uploading files. your existing code should be testing for that value before using any of the uploaded file information and reporting back to the user that the upload failed for any of the other possible values.

for the posted print_r() output, if the code 'isn't working', the problem is something else. do you have php's error related settings setup so that all php errors will get displayed or logged?

Yes errors are turned on, but there aren't any.  Only those I sent here in the Error Log file.

This is an example of the errors, and they are pretty much all like this:

 

mod_fcgid: stderr: PHP Notice: Undefined variable: profilephoto in /var/www/vhosts/site.net/httpdocs/includes/profile.inc on line 253, referer: https://www.site.net/profile

 

<form method='POST' action='' name='signuprocess' enctype='multipart/form-data'>
<input type='hidden' name='updatephoto' value='update'><div class='profile-photo'><h2><div class='profile-icon'><i class='fa fa-camera'></i></div> Profile Photo</h2><br/><div class='signup signup-title'><p><b>Please upload a photo of yourself, not your family.</p></div><div class='signup form-field'><input name='profilephoto' type='file' size='15' /></div>
<input type='submit' value='Upload'></form>

This is the form.

It was working up until a few days ago, so I am suspicious of something changing on the server, through some auto update.

It uploads the file fine, when running locally.  Both are on PHP 7.2.

if (isset($updatephoto))
{
 define ("MAX_SIZE","5000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 
    $image = $_FILES["profilephoto"]["name"];
    $uploadedfile = $_FILES['profilephoto']['tmp_name'];
    
    if ($image) 
    {

        $filename = stripslashes($_FILES['profilephoto']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
echo "test here $uploadedfile";

If I add the final line in this grab of the code, it does not show anything on screen.

So it's as if $_FILES["profilephoto"]["name"] just isn't being seen.

Glad you solved it.

BTW - Here is a more 'proper' way to structure your code:

	define ("MAX_SIZE","5000");
function getExtension($str) 
{
    $i = strrpos($str,".");
    if (!$i) 
    { 
        return ""; 
    }
    $ext = substr($str,$i+1);
    return $ext;
}
//********  Begin main body ************
if (isset($updatephoto))
{
    $errors = 0;
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $image = $_FILES["profilephoto"]["name"];
        $uploadedfile = $_FILES['profilephoto']['tmp_name'];
        if ($image) 
        {
            $filename = stripslashes($_FILES['profilephoto']['name']);
            $extension = getExtension($filename);
            $extension = strtolower($extension);
            echo "test here $uploadedfile";
        }
    }
}    
	

Note:  you don't bury a function def inside your logic.

Of course for a script with multiple functions, I keep them at the end so that I don't have to wade thru them constantly when editing the script.

Edited by ginerjm
On 3/10/2020 at 8:29 PM, simona6 said:

adding photo,Array ( [name] => profilepic.jpeg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpB923.tmp [error] => 0 [size] => 152127 )

Hi,

We have been using this code for ages, but suddenly for some reason we cannot upload an image using the code below.

When we try to Print_r the $profilephoto variable, I get this error.

The filename is correct, but what is that [error]??  And how do I resolve it?

Oddly it does upload it locally, but LIVE, it won't 

This is the code.


if (isset($updatephoto))
{
echo "adding photo,";
print_r($profilephoto);
 define ("MAX_SIZE","5000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    $image =$_FILES["profilephoto"]["name"];
    $uploadedfile = $_FILES['profilephoto']['tmp_name'];

    if ($image) 
    {

        $filename = stripslashes($_FILES['profilephoto']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);


 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {
            echo "Unknown Extension..!";
        }
        else
        {

 $size=filesize($_FILES['profilephoto']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
    echo "File Size Excedeed..!!";
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['profilephoto']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['profilephoto']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
echo $scr;
}

list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$pic=($_FILES['profilephoto']['name']);
$random = (rand()%99999999);
$newname="$random"."$pic";
$filename = "images/profiles/". $newname;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}}
}
$query = ("UPDATE users SET profilephoto =:newname WHERE id =:userid");
$result = $pdo->prepare($query);
$result->execute(array(':userid' => $userid, ':newname' => $newname));
echo "<script>
          window.location.replace('/profile/')
          </script>";}

 

I think you have updated your PHP version so you need to check your server setting.

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.