SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
-
Dec 19, 2005, 14:07 #1
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Renaming and saving an image, please help
Hello,
I get the following errorr when i try and save an image file and its thumb with a new filename, that filenmae being the uploaders 'username'.
"Parse error: parse error, unexpected T_IF in /home/vhosts/therecital.hf4l.com/files/register2.php on line 50"
I really dont understand why, the 'username' is a simple url variable which successfully carriees to the page. Please help. Please also just have a peak at my other post http://www.sitepoint.com/forums/showthread.php?t=327768 dealing with this script. I'm proper stuck,
Thanx again
Tom
Code:if(isset($_POST['Submit'])) { $size = 150; // the thumbnail height $filedir = 'files/'; // the directory for the original image $thumbdir = 'files/'; // the directory for the thumbnail image $prefix = 'small_'; // 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 = $_GET['username'] if (isset($_GET['username'])) { $prod_img = $filedir.$userfile; $prod_img_thumb = $thumbdir.$prefix.$userfile; 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'); 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); } echo ' <a href="'.$prod_img.'"> <img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> </a>'; }else{ echo ' <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form>'; }
-
Dec 19, 2005, 14:45 #2
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
parse error
I don't think the problem is with the variable. The error suggests an unexpected "if". This part of the code shows a missing semi-colon before the "if".
PHP Code:$userfile = $_GET['username']
if (isset($_GET['username']))
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 19, 2005, 15:12 #3
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm afraid the page doesnt seem to load anything after inserting that semi collon which is sposed to be there , doh!
-
Dec 19, 2005, 15:30 #4
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Problem in Creating image, temp file?
After a bit of twaeking now i get a problem in creating iamge errorr, i think it maybe something to do with the temp image that is created , please help
-
Dec 19, 2005, 18:32 #5
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
permissions
0666 does not give "execute" permission, so the script may not be able to run. Maybe try 0777 just to see and then change it back later.
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 19, 2005, 18:41 #6
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have 0777 enabled also :-( Please help
-
Dec 19, 2005, 19:03 #7
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
bug hunting
If you temporarily comment out the line throwing the error and echo the variables it's using, you should be able to eventually trace the problem back to the source. For example, try something like this
PHP Code://$destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
echo "new_width: " . $new_width . " new_height: " . $new_height;
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 19, 2005, 19:11 #8
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know it's the filenaming thats causing the problem, as it works perfectly when i dont try to assign the username field as the filename, but i really need to do this, i think im just doin it wrong
-
Dec 20, 2005, 04:59 #9
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Still no luck guys, please help, let me know if u want more code
-
Dec 20, 2005, 08:17 #10
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
error
By commenting out lines and using echo, you will be able to (eventually) debug your code. What where the values given in the echo? Is the problem with the line I used in my previous post? In your post # 4 you said
Originally Posted by tom_oliverj
Are you not getting that error anymore? If by "not using" the variable you mean that the page request does not have the GET variable, then you are simply by-passing the if isset conditional.PHP Code:if (isset($_GET['username'])) {
$prod_img = $filedir.$userfile;
$prod_img_thumb = $thumbdir.$prefix.$userfile;
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');
ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srci mg),ImageSY($srcimg)) or die('Problem In resizing');
ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
imagedestroy($destimg);
}
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 20, 2005, 08:24 #11
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi, thanx for you patience with me, I'm sorry I wasnt clearer, what i mean was that if i just use the files original name i.e. "$userfile_name = $_FILES['image']['name'];" then it produces the images just fine. The problem comes when i need to change that variable to the username as with my first post, I noated the missing ";" but I'm afraid it still didnt work
the code works fine with just the original name as below,
Code:$userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; if(isset($_FILES['image']['name'])) { $prod_img = $filedir.$userfile_name; $prod_img_thumb = $thumbdir.$prefix.$userfile_name; move_uploaded_file($userfile_tmp, $prod_img); chmod($prod_img, octdec($mode));
Please help
-
Dec 20, 2005, 08:52 #12
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ermm i think i might be geting somwhere with the following code, this now changes the filename to literally '$userfile_test' , how do i now get the value of '$userfile_test' to be displayed instead ???. I know the code looks messy, i dont know what the heck i'm doin really, but its the furthest ive got so far , please help
Tom
Code:$userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; $userfile_test = $_GET['username']; $userfile = $_GET['username'] . '$userfile_test'; if (isset($_FILES['image']['name'])) { $prod_img = $filedir.$userfile; $prod_img_thumb = $thumbdir.$prefix.$userfile; move_uploaded_file($userfile_tmp, $prod_img); chmod($prod_img, octdec($mode));
-
Dec 20, 2005, 09:26 #13
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
PHP strings
PHP parses content between single quotes as literal text. You should not use them for variables. The code you are posting now does not appear in the code you first posted. Please post the entire code that you have now so we can be working on the same code.
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 20, 2005, 10:33 #14
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, the code thus far is , this produces "arrayusername" as the filename, again not the actual value of the username
Code:if(isset($_POST['submit'])) { // the thumbnail height $size = 150; // the directory where the original uploaded image is saved $filedir = 'files/'; // the directory where the thumbnail image is saved $thumbdir = 'files/'; // the prefix to be added to the original name to name the thumbnail $prefix = 'small_'; // the file settings for the uploaded image $mode = '0666'; $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; $userfile = $_GET . 'username'; if (isset($_FILES['image']['name'])) { $prod_img = $filedir.$userfile; $prod_img_thumb = $thumbdir.$prefix.$userfile; 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_w = $sizes[0]; $new_h = $sizes[1]; }else{ $new_h = $size; $new_w = abs($new_h/$aspect_ratio); } $destimg=imagecreatetruecolor($new_w,$new_h) or die('Problem In Creating image'); $srcimg=imagecreatefromjpeg($prod_img) or die('Problem In opening Source Image'); imagecopyresized($destimg,$srcimg,0,0,0,0,$new_w,$new_h,imagesx($srcimg),imagesy($srcimg)) or die('Problem In resizing'); imagejpeg($destimg,$prod_img_thumb,90) or die('Problem In saving'); imagedestroy($destimg); } echo ' <a href="'.$prod_img.'"> <img src="'.$prod_img_thumb.'" width="'.$new_w.'" heigt="'.$new_h.'" alt="" /> </a>'; // show an upload form to upload and resize an image }else{ echo ' <form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="submit" name="submit" value="upload and resize image" /> </form>'; } ?>
-
Dec 21, 2005, 18:52 #15
- Join Date
- Dec 2005
- Posts
- 251
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Still havent cracked it I'm afraid, please help guys
-
Dec 21, 2005, 22:15 #16
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
debugging
I tried a modified version to see if I could figure out what was happening but my web host ipowerweb no longer provides the GD library so I can't use any image functions. You had 2 typos that I noticed
$userfile = $_GET . 'username'; - should be - $_GET['username']
and you missed an "h" - heigt="'.$new_h.'" alt="" />
Try this file to see what you get.
PHP Code:<?php
if(isset($_POST['submit'])){
$size = 150; // the thumbnail height
$filedir = 'files/'; // the directory where the original uploaded image is saved
$thumbdir = 'files/'; // the directory where the thumbnail image is saved
$prefix = 'small_'; // the prefix to be added to the original name to name the thumbnail
$mode = '0666'; // the file settings for the uploaded image
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
$userfile = $_POST['username'];
echo "size = 150 : " . $size . "<br />";
echo "filedir = 'files/' : " . $filedir . "<br />";
echo "thumbdir = 'files/' : " . $thumbdir . "<br />";
echo "prefix = 'small_' : " . $prefix . "<br />";
echo "mode = '0666' : " . $mode . "<br />";
echo "userfile_name FILES image name : " . $userfile_name . "<br />";
echo "userfile_tmp FILES image tmp_name : " . $userfile_tmp . "<br />";
echo "userfile_size FILES image size : " . $userfile_size . "<br />";
echo "userfile_type FILES image type : " . $userfile_type . "<br />";
echo "userfile POST username : " . $userfile . "<br />";
echo "FILES image name : " . $_FILES['image']['name'] . "<br />";
if (isset($_FILES['image']['name'])){
$prod_img = $filedir.$userfile;
echo "prod img : " . $prod_img . "<br />";
$prod_img_thumb = $thumbdir.$prefix.$userfile;
echo "prod img thumb : " . $prod_img_thumb . "<br />";
move_uploaded_file($userfile_tmp, $prod_img);
chmod($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
echo "sizes : " . $sizes . "<br />";
echo "sizes[0] : " . $sizes[0] . "<br />";
echo "sizes[1] : " . $sizes[1] . "<br />";
$aspect_ratio = $sizes[1]/$sizes[0];
echo "aspect_ratio = sizes[1]/sizes[0]: " . $aspect_ratio . "<br />";
if ($sizes[1] <= $size){
$new_w = $sizes[0];
echo "new_w = sizes[0] : " . $new_w . "<br />";
$new_h = $sizes[1];
echo "new_h = sizes[0] : " . $new_h . "<br />";
}else{
$new_h = $size;
echo "new_h = sizes : " . $new_h . "<br />";
$new_w = abs($new_h/$aspect_ratio);
echo "new_w = abs(new_h/aspect_ratio) : " . $new_w . "<br />";
}
$destimg=imagecreatetruecolor($new_w,$new_h)or die('Problem In Creating image');
$srcimg=imagecreatefromjpeg($prod_img)or die('Problem In opening Source Image');
imagecopyresized($destimg,$srcimg,0,0,0,0,$new_w,$new_h,imagesx($srcimg),imagesy($srcimg))or die('Problem In resizing');
imagejpeg($destimg,$prod_img_thumb,90)or die('Problem In saving');
imagedestroy($destimg);
}
echo '<a href="'.$prod_img.'"><img src="'.$prod_img_thumb.'" width="'.$new_w.'" height="'.$new_h.'" alt="" /></a>';
}else{ // show an upload form to upload and resize an image
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">username: <input type="text" name="username" /><input type="file" name="image" /><input type="submit" name="submit" value="upload and resize image" /></form>';
}
?>Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
Bookmarks