jaymeh Posted January 16, 2012 Share Posted January 16, 2012 I am trying to upload files to a user profile system. here is the profile page <?php include('core/init.inc.php'); if (isset($_POST['email'], $_POST['location'], $_POST['about'])) { $errors = array(); if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) { $errors[] = "The email address you entered is not valid"; } if(preg_match('#^[a-z0-9 ]+$#i',$_POST['location'])===0) { $errors[] = 'Your location must only contain A-Z 0-9 and spaces.'; } if (empty($_FILES['avatar']['tmp_name']) === false) { $file_ext = end(explode('.', $_FILES['avatar']['name'])); if(in_array(strtolower($file_ext), array('jpg', 'jpeg', 'gif', 'png')) === false) { $errors[] = 'Your avatar must be an image.'; } } if(empty($errors)) { print_r($_FILES); set_profile_info($_POST['email'],$_POST['location'],$_POST['about'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']); } $userinfo = array( 'email' => htmlentities($_POST['email']), 'location' => htmlentities($_POST['location']), 'about' => htmlentities($_POST['about']) ); } else { $userinfo = fetch_user_info($_SESSION['uid']); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Edit your Profile</title> </head> <body> <div> <?php if(isset($errors) == false) { echo 'Click update to edit your profile.'; } else if(empty($errors)) { echo 'Your profile has been updated.'; } else { echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>'; } ?> </div> <form action="" method="post" enctype="multipart/form-data"> <div> <label for="email">Email: </label> <input type="text" name="email" id="email" value="<?php echo $userinfo['email']; ?>" /> </div> <div> <label for="location">Location: </label> <input type="text" name="location" id="location" value="<?php echo $userinfo['location']; ?>" /> </div> <div> <label for="about">About Me: </label> <textarea name="about" id="about" rows="14" cols="50"><?php echo strip_tags($userinfo['about']); ?></textarea> </div> <div> <label for="avatar">Avatar: </label> <input type="file" name="avatar" id="avatar"/> </div> <div> <input type="submit" value="Update" /> </div> </form> </body> </html> here is the function taken from an external file function set_profile_info($email, $location,$about,$avatar) { $email = mysql_escape_string(htmlentities($email)); $about = mysql_escape_string(nl2br(htmlentities($about))); $location = mysql_escape_string($location); if (file_exists($avatar)) { $src_size = getimagesize($avatar); if ($src_size['mime'] === 'image/jpeg') { $src_img = imagecreatefromjpeg($avatar); } else if ($src_size['mime'] === 'image/png') { $src_img = imagecreatefrompng($avatar); } else if ($src_size['mime'] === 'image/gif') { $src_img = imagecreatefromgif($avatar); } else { $src_img = false; } if ($src_img !== false) { $thumb_width= 200; if($src_size[0] <= $thumb_width) { $thumb = $src_img; } else { $new_size[0] = $thumb_width; $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width; $thumb = imagecreatetruecolor($new_size[0], $new_size[1]); imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]); } imagejpeg($thumb, "{$GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpg"); } } $sql = "UPDATE `users` SET `user_email` = '{$email}', `user_about` = '{$about}', `user_location` = '{$location}' WHERE `user_id` = {$_SESSION['uid']}"; mysql_query($sql); } Below I have returned the array of files to check if its been uploaded correctly. Array ( [avatar] => Array ( [name] => Sonic.jpg [type] => image/jpeg [tmp_name] => /var/tmp/php.waq8n [error] => 0 => 48477 ) ) But I get this error message. Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/php.waq8n' is not a valid JPEG file in /web/stud/u0963643/userprofilesection/finaluserprofile/core/inc/user.inc.php on line 71 If someone could point out where in this code I have made an error I would be very grateful Thanks Jamie Link to comment https://forums.phpfreaks.com/topic/255134-uploading-and-resizing-images-for-a-user-profile-system/ Share on other sites More sharing options...
xProteuSx Posted January 16, 2012 Share Posted January 16, 2012 I know that its not exactly an answer to your question, but if you're willing ton consider an alternative: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ Link to comment https://forums.phpfreaks.com/topic/255134-uploading-and-resizing-images-for-a-user-profile-system/#findComment-1308139 Share on other sites More sharing options...
laffin Posted January 16, 2012 Share Posted January 16, 2012 take a close look if ($src_size['mime'] === 'image/jpeg') and Array ( [avatar] => Array ( [name] => Sonic.jpg [type] => image/jpeg [tmp_name] => /var/tmp/php.waq8n [error] => 0 [size] => 48477 ) ) there is no 'mime' element in the array, it's 'type' Link to comment https://forums.phpfreaks.com/topic/255134-uploading-and-resizing-images-for-a-user-profile-system/#findComment-1308141 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 Are you sure the file itself, before you upload it, is actually a valid jpg/jpeg image file? Have you tried a different type of image file? @laffin, the $src_size data is from a getimagesize statement. Edit: I just tried the code you posted for a known good .jpg source file and it did create an expected resized .jpg image in the appropriate destination folder. I would check to make sure your sonic.jpg is actually a .jpg file (browsers will open mis-named images correctly, php functions are not going to be as forgiving.) Link to comment https://forums.phpfreaks.com/topic/255134-uploading-and-resizing-images-for-a-user-profile-system/#findComment-1308144 Share on other sites More sharing options...
jaymeh Posted January 16, 2012 Author Share Posted January 16, 2012 Thanks. I have tried it with a few different .jpg's and it seems a bit hit and miss if I'm honest. But I have come across a problem in the actual code itself anyway. Since I will be using the code for an iphone web app I came across the problem that input type="file". Will not work with iphones. I have decided to remove this feature altogether since the work around involves each user downloading a specific iphone app which handles this. Thanks again for your help Jamie Link to comment https://forums.phpfreaks.com/topic/255134-uploading-and-resizing-images-for-a-user-profile-system/#findComment-1308169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.