This is a discussion on file uploads within the PHP General forums, part of the PHP Programming Forums category; Hi, I have a file upload problem, but I don't think it's a permission thing (if only, that ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I have a file upload problem, but I don't think it's a permission thing (if only, that would be simple!). I have written a resizing method for a class I'm working on, and it always fails on the imageJpeg() at the end of the method. This is the error I get... imagejpeg(): Unable to open '/Library/WebServer/Documents/my_site/_lib/_products/2_big_me.jpg' for writing in /Library/WebServer/Documents/my_site/_lib/_classes/class.products.php on line 132 Now, here's the strange bit, if I call my method and just use move_uploaded_file() then the image is saved correctly, so**I know it's not the permissions, could someone have a little look**at my method and let me know if there's anything wrong with it please // $ID is an integer used for naming purposes // $aImage is an array, it's basically a copy of $_FILES["image"] // BASE_DIR is a constant var and holds, yep you guessed it, the base dir of the site! // IMGMAXHEIGHT & IMGMAXWIDTH are also constants used for resizing purposes function storeBigImage($ID, $aImage){ ********// create filenames ********fopen($aImage['name'], 'r'); ********$aNewImage['real_name'] = $aImage['name']; ********$aNewImage['new_name'] = $ID . '_' . 'big' . '_' . $aNewImage['real_name']; ********$aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' ..**$aNewImage['new_name']; ********* ********// copy original image ********$aNewImage['original_image'] = ImageCreateFromJpeg($aImage['tmp_name']); ********$aNewImage['sizes'] = getimagesize($aImage['tmp_name']); ********$aNewImage['width'] = $aNewImage['sizes'][0]; ********$aNewImage['height'] = $aNewImage['sizes'][0]; ******** ********if($aNewImage['width'] >= IMGMAXWIDTH || $aNewImage['height'] >= IMGMAXHEIGHT){ ************ ************// calculate ratios ************$iRatio_w = IMGMAXWIDTH / $aNewImage['width']; ************$iRatio_h = IMGMAXHEIGHT / $aNewImage['height']; ************$iRatio = $iRatio_w < $iRatio_h ? $iRatio_w:$iRatio_h; ************ ************// calculate new dimensions ************$aNewImage['new_width'] = $aNewImage['width'] * $iRatio; ************$aNewImage['new_height'] = $aNewImage['height'] * $iRatio; ************ ************// save resized image ************$aNewImage['new_image'] = ImageCreateTrueColor($aNewImage['new_width'], $aNewImage['new_height']); ************ImageCopyResized($aNewImage['new_image'], ImageCreateFromJpeg($aImage['tmp_name']), 0, 0, 0, 0, $aNewImage['new_width'], $aNewImage['new_height'], $aNewImage['width'], $aNewImage['height']); ************//ImageJpeg($aNewImage['new_image'], $aNewImage['image_loc']); ************ImageJpeg($aNewImage['original_image'], $aNewImage['image_loc']); ******** ********} else { ******** ************// save original image ************//ImageJpeg($aImage['tmp_name'], $aNewImage['image_loc']); ************ImageJpeg($aNewImage['original_image'], $aNewImage['image_loc']); ************//ImageJpeg($this->aArgs['Image']['tmp_name'], $aNewImage['image_loc']); ********} ****} I have ftp'd into my local server and the folder in question is set at 777, and like I said it works fine if I don't use this class Any ideas ?? Thanks, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ |
|
|||
|
Just to clarify about using move_uploaded_file()
I call the storeBigImage methid from aother method like so function addProduct(){ $this->storeBigImage($ID, $_FILES["image"]); } that doesn't work, if I use this: function addProduct(){ // Move the uploaded file to the correct location move_uploaded_file($$_FILES["image"]["tmp_name"], BASE_DIR."/_img/_products/".$iProductId."_".$fileName); } The above works fine, which I find very strange indeed! Thanks, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ On 1 Dec 2003, at 12:50, Jon Bennett wrote: > Hi, > > I have a file upload problem, but I don't think it's a permission > thing (if only, that would be simple!). > > I have written a resizing method for a class I'm working on, and it > always fails on the imageJpeg() at the end of the method. This is the > error I get... > > imagejpeg(): Unable to open > '/Library/WebServer/Documents/my_site/_lib/_products/2_big_me.jpg' for > writing in > /Library/WebServer/Documents/my_site/_lib/_classes/class.products.php > on line 132 > > Now, here's the strange bit, if I call my method and just use > move_uploaded_file() then the image is saved correctly, so**I know > it's not the permissions, could someone have a little look**at my > method and let me know if there's anything wrong with it please > > > // $ID is an integer used for naming purposes > // $aImage is an array, it's basically a copy of $_FILES["image"] > // BASE_DIR is a constant var and holds, yep you guessed it, the base > dir of the site! > // IMGMAXHEIGHT & IMGMAXWIDTH are also constants used for resizing > purposes > > function storeBigImage($ID, $aImage){ > > ********// create filenames > ********fopen($aImage['name'], 'r'); > ********$aNewImage['real_name'] = $aImage['name']; > ********$aNewImage['new_name'] = $ID . '_' . 'big' . '_' . > $aNewImage['real_name']; > ********$aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' > .**$aNewImage['new_name']; > ********* > ********// copy original image > ********$aNewImage['original_image'] = > ImageCreateFromJpeg($aImage['tmp_name']); > ********$aNewImage['sizes'] = getimagesize($aImage['tmp_name']); > ********$aNewImage['width'] = $aNewImage['sizes'][0]; > ********$aNewImage['height'] = $aNewImage['sizes'][0]; > ******** > ********if($aNewImage['width'] >= IMGMAXWIDTH || $aNewImage['height'] > >= IMGMAXHEIGHT){ > ************ > ************// calculate ratios > ************$iRatio_w = IMGMAXWIDTH / $aNewImage['width']; > ************$iRatio_h = IMGMAXHEIGHT / $aNewImage['height']; > ************$iRatio = $iRatio_w < $iRatio_h ? $iRatio_w:$iRatio_h; > ************ > ************// calculate new dimensions > ************$aNewImage['new_width'] = $aNewImage['width'] * $iRatio; > ************$aNewImage['new_height'] = $aNewImage['height'] * $iRatio; > ************ > ************// save resized image > ************$aNewImage['new_image'] = > ImageCreateTrueColor($aNewImage['new_width'], > $aNewImage['new_height']); > ************ImageCopyResized($aNewImage['new_image'], > ImageCreateFromJpeg($aImage['tmp_name']), 0, 0, 0, 0, > $aNewImage['new_width'], $aNewImage['new_height'], > $aNewImage['width'], $aNewImage['height']); > ************//ImageJpeg($aNewImage['new_image'], > $aNewImage['image_loc']); > ************ImageJpeg($aNewImage['original_image'], > $aNewImage['image_loc']); > ******** > ********} else { > ******** > ************// save original image > ************//ImageJpeg($aImage['tmp_name'], $aNewImage['image_loc']); > ************ImageJpeg($aNewImage['original_image'], > $aNewImage['image_loc']); > ************//ImageJpeg($this->aArgs['Image']['tmp_name'], > $aNewImage['image_loc']); > ********} > ****} > > > I have ftp'd into my local server and the folder in question is set at > 777, and like I said it works fine if I don't use this class > > Any ideas ?? > > Thanks, > > Jon > > > jon bennett | jon@jben.net > new media designer / developer > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > J b e n . n e t > > 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD > t: +44 (0) 1225 341039 w: http://www.jben.net/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |
|
|||
|
PHP first gives the file a temporary name on a temporary location. This
name is not the same as the name given in the upload form. I think the temp filename is in the $_FILES array too, do a print_r($_FILES) to check. >Now, here's the strange bit, if I call my method and just use >move_uploaded_file() then the image is saved correctly, so I know it's >not the permissions, could someone have a little look at my method and >let me know if there's anything wrong with it please > |
|
|||
|
Jon Bennett wrote:
> Just to clarify about using move_uploaded_file() > > ... > > function addProduct(){ > > // Move the uploaded file to the correct location > move_uploaded_file($$_FILES["image"]["tmp_name"], > BASE_DIR."/_img/_products/".$iProductId."_".$fileName); > } > > ... > >> function storeBigImage($ID, $aImage){ >> >> // create filenames >> fopen($aImage['name'], 'r'); >> $aNewImage['real_name'] = $aImage['name']; >> $aNewImage['new_name'] = $ID . '_' . 'big' . '_' . >> $aNewImage['real_name']; >> $aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' >> . $aNewImage['new_name']; >> >> ... Just noticed one thing... maybe it's just a typo, but directory, where You save uploaded image isn't the same in those examples. In "move_uploaded_file()" it contains "_img/...", but in "storeBigImage()" there is "_lib/...". And therefore first case is working and second isn't. -- Pavel a.k.a. Papi |
|
|||
|
well I'll be dammed, that was it! Geeze, you look at something for so
long sometimes you can't see the wood for the trees!!!! I feel sooooo stoopid now! Thanks, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ On 1 Dec 2003, at 15:10, Pavel Jartsev wrote: > Jon Bennett wrote: >> Just to clarify about using move_uploaded_file() >> ... >> function addProduct(){ >> // Move the uploaded file to the correct location >> move_uploaded_file($$_FILES["image"]["tmp_name"], >> BASE_DIR."/_img/_products/".$iProductId."_".$fileName); >> } >> ... >> >>> function storeBigImage($ID, $aImage){ >>> >>> // create filenames >>> fopen($aImage['name'], 'r'); >>> $aNewImage['real_name'] = $aImage['name']; >>> $aNewImage['new_name'] = $ID . '_' . 'big' . '_' . >>> $aNewImage['real_name']; >>> $aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' . >>> $aNewImage['new_name']; >>> ... > > > Just noticed one thing... maybe it's just a typo, but directory, where > You save uploaded image isn't the same in those examples. > > In "move_uploaded_file()" it contains "_img/...", but in > "storeBigImage()" there is "_lib/...". And therefore first case is > working and second isn't. > > > -- > Pavel a.k.a. Papi > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |
|
|||
|
I'm trying to return a value if the file uploads correctly, using
return, but I don't seem to be getting anything back. I've added this to my storeImages method return = $aNewImage['new_name']; and when I call the method from my addProduct() method I use this: $sThumbnailFileName = $this->_storeImages($iProductId, $aArgs["Image"], 'thumb'); I then assumed I'd be able to just reference the $sThumbnailFileName var so I can insert the filename into the db, but it always goes in blank, does $sThumbnailFileName not get returned $aNewImage['new_name'], or is it stuck in an array or something ?? Thanks, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ On 1 Dec 2003, at 15:26, Jon Bennett wrote: > well I'll be dammed, that was it! Geeze, you look at something for so > long sometimes you can't see the wood for the trees!!!! > > I feel sooooo stoopid now! > > Thanks, > > Jon > > > jon bennett | jon@jben.net > new media designer / developer > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > J b e n . n e t > > 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD > t: +44 (0) 1225 341039 w: http://www.jben.net/ > > > On 1 Dec 2003, at 15:10, Pavel Jartsev wrote: > >> Jon Bennett wrote: >>> Just to clarify about using move_uploaded_file() >>> ... >>> function addProduct(){ >>> // Move the uploaded file to the correct location >>> move_uploaded_file($$_FILES["image"]["tmp_name"], >>> BASE_DIR."/_img/_products/".$iProductId."_".$fileName); >>> } >>> ... >>> >>>> function storeBigImage($ID, $aImage){ >>>> >>>> // create filenames >>>> fopen($aImage['name'], 'r'); >>>> $aNewImage['real_name'] = $aImage['name']; >>>> $aNewImage['new_name'] = $ID . '_' . 'big' . '_' . >>>> $aNewImage['real_name']; >>>> $aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' . >>>> $aNewImage['new_name']; >>>> ... >> >> >> Just noticed one thing... maybe it's just a typo, but directory, >> where You save uploaded image isn't the same in those examples. >> >> In "move_uploaded_file()" it contains "_img/...", but in >> "storeBigImage()" there is "_lib/...". And therefore first case is >> working and second isn't. >> >> >> -- >> Pavel a.k.a. Papi >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |
|
|||
|
sorry, made a mistake when writing my email:
return = $aNewImage['new_name']; should be: return $aNewImage['new_name']; Cheers, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ On 1 Dec 2003, at 15:57, Jon Bennett wrote: > I'm trying to return a value if the file uploads correctly, using > return, but I don't seem to be getting anything back. > > I've added this to my storeImages method > > return = $aNewImage['new_name']; > > and when I call the method from my addProduct() method I use this: > > $sThumbnailFileName = $this->_storeImages($iProductId, > $aArgs["Image"], 'thumb'); > > I then assumed I'd be able to just reference the $sThumbnailFileName > var so I can insert the filename into the db, but it always goes in > blank, does $sThumbnailFileName not get returned > $aNewImage['new_name'], or is it stuck in an array or something ?? > > Thanks, > > Jon > > > jon bennett | jon@jben.net > new media designer / developer > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > J b e n . n e t > > 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD > t: +44 (0) 1225 341039 w: http://www.jben.net/ > > > On 1 Dec 2003, at 15:26, Jon Bennett wrote: > >> well I'll be dammed, that was it! Geeze, you look at something for so >> long sometimes you can't see the wood for the trees!!!! >> >> I feel sooooo stoopid now! >> >> Thanks, >> >> Jon >> >> >> jon bennett | jon@jben.net >> new media designer / developer >> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ >> >> J b e n . n e t >> >> 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD >> t: +44 (0) 1225 341039 w: http://www.jben.net/ >> >> >> On 1 Dec 2003, at 15:10, Pavel Jartsev wrote: >> >>> Jon Bennett wrote: >>>> Just to clarify about using move_uploaded_file() >>>> ... >>>> function addProduct(){ >>>> // Move the uploaded file to the correct location >>>> move_uploaded_file($$_FILES["image"]["tmp_name"], >>>> BASE_DIR."/_img/_products/".$iProductId."_".$fileName); >>>> } >>>> ... >>>> >>>>> function storeBigImage($ID, $aImage){ >>>>> >>>>> // create filenames >>>>> fopen($aImage['name'], 'r'); >>>>> $aNewImage['real_name'] = $aImage['name']; >>>>> $aNewImage['new_name'] = $ID . '_' . 'big' . '_' . >>>>> $aNewImage['real_name']; >>>>> $aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' . >>>>> $aNewImage['new_name']; >>>>> ... >>> >>> >>> Just noticed one thing... maybe it's just a typo, but directory, >>> where You save uploaded image isn't the same in those examples. >>> >>> In "move_uploaded_file()" it contains "_img/...", but in >>> "storeBigImage()" there is "_lib/...". And therefore first case is >>> working and second isn't. >>> >>> >>> -- >>> Pavel a.k.a. Papi >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |
|
|||
|
Down't worry, it's sorted!
Cheers, Jon jon bennett | jon@jben.net new media designer / developer _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ J b e n . n e t 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD t: +44 (0) 1225 341039 w: http://www.jben.net/ On 1 Dec 2003, at 15:57, Jon Bennett wrote: > I'm trying to return a value if the file uploads correctly, using > return, but I don't seem to be getting anything back. > > I've added this to my storeImages method > > return = $aNewImage['new_name']; > > and when I call the method from my addProduct() method I use this: > > $sThumbnailFileName = $this->_storeImages($iProductId, > $aArgs["Image"], 'thumb'); > > I then assumed I'd be able to just reference the $sThumbnailFileName > var so I can insert the filename into the db, but it always goes in > blank, does $sThumbnailFileName not get returned > $aNewImage['new_name'], or is it stuck in an array or something ?? > > Thanks, > > Jon > > > jon bennett | jon@jben.net > new media designer / developer > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > J b e n . n e t > > 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD > t: +44 (0) 1225 341039 w: http://www.jben.net/ > > > On 1 Dec 2003, at 15:26, Jon Bennett wrote: > >> well I'll be dammed, that was it! Geeze, you look at something for so >> long sometimes you can't see the wood for the trees!!!! >> >> I feel sooooo stoopid now! >> >> Thanks, >> >> Jon >> >> >> jon bennett | jon@jben.net >> new media designer / developer >> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ >> >> J b e n . n e t >> >> 91 Gloucester Rd, Trowbridge, Wilts, BA14 0AD >> t: +44 (0) 1225 341039 w: http://www.jben.net/ >> >> >> On 1 Dec 2003, at 15:10, Pavel Jartsev wrote: >> >>> Jon Bennett wrote: >>>> Just to clarify about using move_uploaded_file() >>>> ... >>>> function addProduct(){ >>>> // Move the uploaded file to the correct location >>>> move_uploaded_file($$_FILES["image"]["tmp_name"], >>>> BASE_DIR."/_img/_products/".$iProductId."_".$fileName); >>>> } >>>> ... >>>> >>>>> function storeBigImage($ID, $aImage){ >>>>> >>>>> // create filenames >>>>> fopen($aImage['name'], 'r'); >>>>> $aNewImage['real_name'] = $aImage['name']; >>>>> $aNewImage['new_name'] = $ID . '_' . 'big' . '_' . >>>>> $aNewImage['real_name']; >>>>> $aNewImage['image_loc'] = BASE_DIR . '_lib/_products/' . >>>>> $aNewImage['new_name']; >>>>> ... >>> >>> >>> Just noticed one thing... maybe it's just a typo, but directory, >>> where You save uploaded image isn't the same in those examples. >>> >>> In "move_uploaded_file()" it contains "_img/...", but in >>> "storeBigImage()" there is "_lib/...". And therefore first case is >>> working and second isn't. >>> >>> >>> -- >>> Pavel a.k.a. Papi >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |