danposs86 Posted May 1, 2007 Share Posted May 1, 2007 i have this code which uploads an image, resizes it and renames it using the uploaders name, a number indicating which image it is and the original file name. It then writes the file location to a database.It works fantastically...except for one thing!If theres a space in the filename or username, it wont display the images. Can anyone suggest some code that will change all spaces to '%20' just before it gets saved.Heres the PHP: <?phpif(isset($_POST['Submit'])){ $size = 300; // the thumbnail height $sql = "SELECT name FROM ibf_members WHERE id='".$member_id."'"; $query = mysql_query($sql); $row = mysql_fetch_assoc($query); $member_name = $row["name"]; $filedir = 'uploads/temp_images/'; // the directory for the original image $thumbdir = 'uploads/'; // the directory for the thumbnail image $prefix = $member_name.'_1_'; // the prefix to be added to the original name $maxfile = '2000000'; $mode = '0666'; $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; $userfile_type = strtolower($userfile_type); if (isset($_FILES['image']['name'])) { if (($userfile_type != "image/jpg") && ($userfile_type != "image/jpeg") && ($userfile_type != "image/pjpeg")) { echo ' <div class="photo_alert"> <div class="login_box"> ERROR </div> <div class="welcome_text"> There has been a problem uploading your image. This may have been due to two things: <br /><br /> <img src="graphics/bullet_arrow_small_red.gif" /> You tried to upload an image that wasnt a Jpeg, only images with the extension .jpg or .jpeg can be uploaded.<br><br> <img src="graphics/bullet_arrow_small_red.gif" /> You forgot to select a file to be uploaded.<br> </div> </div> <div><br /> <a href="member_upload_photo_1.php">Click here to try again</a> </div>'; } else { $prod_img = $filedir.$userfile_name; $prod_img_thumb = $thumbdir.$prefix.$userfile_name; move_uploaded_file($userfile_tmp, $prod_img); chmod ($prod_img, octdec($mode)); $sizes = getimagesize($prod_img); $aspect_ratio = $sizes[1]/$sizes[0]; if ($sizes[1] <= $size) { $new_width = $sizes[0]; $new_height = $sizes[1]; }else{ $new_height = $size; $new_width = abs($new_height/$aspect_ratio); } $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image'); $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image'); if(function_exists('imagecopyresampled')) { imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); }else{ Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); } ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving'); imagedestroy($destimg); include "log_cookie.php"; $sql = "SELECT name FROM ibf_members WHERE id='".$member_id."'"; $query = mysql_query($sql); $row = mysql_fetch_assoc($query); $member_name = $row["name"]; mysql_query("UPDATE site_cars SET car_image_1 = '$prod_img_thumb', approved = 'n' WHERE user = '$member_name'"); echo "<table width='100%'> <tr> <td><div align='center'>Success ".$member_name."! Photo one has been added.</div></td> </tr> <tr> <td><div align='center'><img src='".$prod_img_thumb."' alt='".$member_name." Photo One'></div></td> </tr> <tr> <td><div align='center'><br /><br /><a href='member_upload_photo_2.php'>Click here to add the second photo >></a></div></td> </tr> </table><br />"; }}}else{ echo ' <div> Now you need to to add four photos to your cars profile.<br /><br /> </div> <div> </div> <div align="center" class="photo_main"> <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> <input type="file" name="image" size="60"> <p class="red_text"><img src="graphics/bullet_arrow_small_red.gif" /> Important!<br />Please make sure that the image you are selecting is a JPEG image with the extension .jpg or .jpeg ONLY. Also dont worry about the file size as the image will be resized once uploaded, the only catch is that it may take a moment to upload a large image.</p> <input type="Submit" name="Submit" value="Upload the first photo"> </form> </div>';}?> Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted May 1, 2007 Share Posted May 1, 2007 Use str_replace() on the values to replace spaces.But here's two notes:Why allow spaces in the username (or is the username theu full name??)I would recommend replacing the spaces with underscores (_) instead of %20, espacially in the filename..You can also use urlencode() to encode the strings "as URL"Tip: use [ codebox] instead of [ code] when posting long/big codes (without the spaces).. ;?)Good Luck and Don't Panic! Link to comment Share on other sites More sharing options...
danposs86 Posted May 2, 2007 Author Share Posted May 2, 2007 the usernames are taken from a forum, some people do/will use their full names.would this work: (middle line) ..........$prod_img_thumb = $thumbdir.$prefix.$userfile_name;$prod_img_thumb = str_replace ( ‘ ‘, ”, $prod_img_thumb );move_uploaded_file($userfile_tmp, $prod_img);......... Link to comment Share on other sites More sharing options...
danposs86 Posted May 2, 2007 Author Share Posted May 2, 2007 actuallt think this: $prod_img_thumb = preg_replace('/\s+/', '_', $prod_img_thumb); might be the better line Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted May 2, 2007 Share Posted May 2, 2007 That would work fine.. :?) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.