Discussion:
Writing a file from a Sub-Domain form to Upper Domain
Don Wieland
2014-08-20 17:13:06 UTC
Permalink
Morning folks,

I have a form/processing page in which I am trying to allow users to UPLOAD an image:

http://admin.wrightcheck.com/admin/resources/php/upload_image.php

and I want to use the "move_uploaded_file()" function to a folder in the upper main domain:

Loading Image...

So far all combination are not working. What is the best way to handle paths with such a routine? Not sure if there is something in my .httpaccess config that is not allowing this.

Thanks!

Don Wieland
D W D a t a C o n c e p t s
Aziz Saleh
2014-08-20 17:19:54 UTC
Permalink
Post by Don Wieland
Morning folks,
http://admin.wrightcheck.com/admin/resources/php/upload_image.php
http://www.wrightcheck.com/media/images/thefilename.jpg
So far all combination are not working. What is the best way to handle
paths with such a routine? Not sure if there is something in my .httpaccess
config that is not allowing this.
Thanks!
Don Wieland
D W D a t a C o n c e p t s
If you show us the code it would be easier to help.
Don Wieland
2014-08-20 17:43:41 UTC
Permalink
Post by Aziz Saleh
If you show us the code it would be easier to help.
This is the error when adding the error_reporting:

Warning: move_uploaded_file(../../../media/images/pic2.jpg): failed to open stream: No such file or directory in /home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpKuJ4td' to '../../../media/images/pic2.jpg' in /home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php on line 40

Here is my code - there are a few debugging echoes though - sorry

//Set the values resulting from the Upload form POST.
$tmp_name = $_FILES['file'] ['tmp_name'];
$name = $_FILES['file'] ['name'];
$size = $_FILES['file'] ['size'];
$type = $_FILES['file'] ['type'];
// $error = $_FILES['file'] ['error'];
$extension = strtolower(substr($name, strpos($name, '.') +1));

// If values are set and not empty move_upload_file is invoked to place the uploaded file into the web server directory identified by $directory.
if (isset($name)) {
// Check for a valid image filetype.
if (!empty($name)) {
// echo 'Filename is not empty.<br><hr>';

if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'png' || $extension == 'gif') {

echo 'Temporary Filename: '.$tmp_name.'<br><hr>';
echo 'Actual Filename: '.$name.'<br><hr>';
$directory = '../../../media/images/';

// $directory = '../../../resources/uploads/'; This fails also so it does not appear to be a permissions issue as we should be able to create a file into this project directory.
$destination = $directory.$name;
echo 'Upload Complete Path: '.$destination.'<br><hr>';
echo '<a href="'.$destination.'" target="_blank">link</a><br><hr>';

if (move_uploaded_file($tmp_name, $destination)) {
// This is failing but I cannot figure out why. I do not get an error message in the error.php file in the server directory /edit_image/error.php like I get any other time there is a php error.
// if (move_uploaded_file($tmp_name, '../../../resources/uploads/uploaded.jpg')) {

// echo 'Image file '.$name.' successfully uploaded to '.$directory.'.';
echo 'Image file successfully uploaded.<br><hr>';
} else {
echo 'move_upload_file failed!<br><hr>';
}

} else {
echo '<span class="formErrorMessage">Image File Type must be jpg, jpeg, png or gif!</span><br><br>';
}

} else {
echo '<span class="formErrorMessage">Please choose a file to upload!</span><br><br>';
}
}

Don Wieland
D W D a t a C o n c e p t s
Stuart Dallas
2014-08-20 17:47:24 UTC
Permalink
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Post by Don Wieland
Post by Aziz Saleh
If you show us the code it would be easier to help.
Warning: move_uploaded_file(../../../media/images/pic2.jpg): failed to
open stream: No such file or directory in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpKuJ4td'
to '../../../media/images/pic2.jpg' in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40
Here is my code - there are a few debugging echoes though - sorry
//Set the values resulting from the Upload form POST.
$tmp_name = $_FILES['file'] ['tmp_name'];
$name = $_FILES['file'] ['name'];
$size = $_FILES['file'] ['size'];
$type = $_FILES['file'] ['type'];
// $error = $_FILES['file'] ['error'];
$extension = strtolower(substr($name, strpos($name, '.') +1));
// If values are set and not empty move_upload_file is invoked to place
the uploaded file into the web server directory identified by $directory.
if (isset($name)) {
// Check for a valid image filetype.
if (!empty($name)) {
// echo 'Filename is not empty.<br><hr>';
if ($extension == 'jpg' || $extension == 'jpeg' ||
$extension == 'png' || $extension == 'gif') {
echo 'Temporary Filename: '.$tmp_name.'<br><hr>';
echo 'Actual Filename: '.$name.'<br><hr>';
$directory = '../../../media/images/';
// $directory = '../../../resources/uploads/'; This
fails also so it does not appear to be a permissions issue as we should be
able to create a file into this project directory.
$destination = $directory.$name;
'.$destination.'<br><hr>';
echo '<a href="'.$destination.'"
target="_blank">link</a><br><hr>';
if (move_uploaded_file($tmp_name, $destination)) {
// This is failing but I cannot figure out why. I
do not get an error message in the error.php file in the server directory
/edit_image/error.php like I get any other time there is a php error.
// if (move_uploaded_file($tmp_name,
'../../../resources/uploads/uploaded.jpg')) {
// echo 'Image file '.$name.' successfully
uploaded to '.$directory.'.';
echo 'Image file successfully
uploaded.<br><hr>';
} else {
echo 'move_upload_file failed!<br><hr>';
}
} else {
echo '<span class="formErrorMessage">Image File
Type must be jpg, jpeg, png or gif!</span><br><br>';
}
} else {
echo '<span class="formErrorMessage">Please choose a file
to upload!</span><br><br>';
}
}
I would always recommend using absolute paths rather than relative, mainly
because it helps debugging.

Change:
Stuart Dallas
2014-08-20 17:49:34 UTC
Permalink
Sorry, hit the wrong button and sent before I'd finished.
Post by Don Wieland
Post by Aziz Saleh
If you show us the code it would be easier to help.
Warning: move_uploaded_file(../../../media/images/pic2.jpg): failed to
open stream: No such file or directory in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpKuJ4td'
to '../../../media/images/pic2.jpg' in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40
Here is my code - there are a few debugging echoes though - sorry
//Set the values resulting from the Upload form POST.
$tmp_name = $_FILES['file'] ['tmp_name'];
$name = $_FILES['file'] ['name'];
$size = $_FILES['file'] ['size'];
$type = $_FILES['file'] ['type'];
// $error = $_FILES['file'] ['error'];
$extension = strtolower(substr($name, strpos($name, '.') +1));
// If values are set and not empty move_upload_file is invoked to place
the uploaded file into the web server directory identified by $directory.
if (isset($name)) {
// Check for a valid image filetype.
if (!empty($name)) {
// echo 'Filename is not empty.<br><hr>';
if ($extension == 'jpg' || $extension == 'jpeg' ||
$extension == 'png' || $extension == 'gif') {
echo 'Temporary Filename: '.$tmp_name.'<br><hr>';
echo 'Actual Filename: '.$name.'<br><hr>';
$directory = '../../../media/images/';
// $directory = '../../../resources/uploads/'; This
fails also so it does not appear to be a permissions issue as we should be
able to create a file into this project directory.
$destination = $directory.$name;
'.$destination.'<br><hr>';
echo '<a href="'.$destination.'"
target="_blank">link</a><br><hr>';
if (move_uploaded_file($tmp_name, $destination)) {
// This is failing but I cannot figure out why. I
do not get an error message in the error.php file in the server directory
/edit_image/error.php like I get any other time there is a php error.
// if (move_uploaded_file($tmp_name,
'../../../resources/uploads/uploaded.jpg')) {
// echo 'Image file '.$name.' successfully
uploaded to '.$directory.'.';
echo 'Image file successfully
uploaded.<br><hr>';
} else {
echo 'move_upload_file failed!<br><hr>';
}
} else {
echo '<span class="formErrorMessage">Image File
Type must be jpg, jpeg, png or gif!</span><br><br>';
}
} else {
echo '<span class="formErrorMessage">Please choose a file
to upload!</span><br><br>';
}
}
I would always recommend using absolute paths rather than relative, mainly
because it helps debugging.

Change:

$directory = '../../../media/images/';

to:

$directory = dirname(__FILE__).'../../../media/images/';

And check the path in the error message is correct.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Don Wieland
2014-08-20 17:53:24 UTC
Permalink
Post by Stuart Dallas
$directory = dirname(__FILE__).'../../../media/images/';
And check the path in the error message is correct.
When i do this I get this error:

Warning: move_uploaded_file(/home/rtsadven/public_html/wrightcheck/admin/resources/php../../../media/images/pic2.jpg): failed to open stream: No such file or directory in /home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/php3buVDQ' to '/home/rtsadven/public_html/wrightcheck/admin/resources/php../../../media/images/pic2.jpg' in /home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php on line 40


???

Don Wieland
D W D a t a C o n c e p t s
~~~~~~~~~~~~~~~~~~~~~~~~~
***@dwdataconcepts.com
http://www.dwdataconcepts.com
Direct Line - (949) 336-4828
SKYPE - skypename = dwdata

Integrated data solutions to fit your business needs.

Need assistance in dialing in your FileMaker solution? Check out our Developer Support Plan at:
http://www.dwdataconcepts.com/DevSup.php

Appointment 1.0v9 - Powerful Appointment Scheduling for FileMaker Pro 9 or higher
http://www.appointment10.com

For a quick overview -
http://www.appointment10.com/Appt10_Promo/Overview.html
Stuart Dallas
2014-08-20 17:56:50 UTC
Permalink
Post by Don Wieland
Post by Stuart Dallas
$directory = dirname(__FILE__).'../../../media/images/';
And check the path in the error message is correct.
failed to open stream: No such file or directory in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/php3buVDQ'
to
'/home/rtsadven/public_html/wrightcheck/admin/resources/php../../../media/images/pic2.jpg'
in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40
My bad. Add a / before the first .. on that line:

$directory = dirname(__FILE__).'/../../../media/images/';

However, from that error message you can see the full path that will be
generated:

/home/rtsadven/public_html/wrightcheck/admin/resources/php/../../../media/images/pic2.jpg

Or, normalised:

/home/rtsadven/public_html/wrightcheck/media/images/pic2.jpg

Is that the right path?

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Don Wieland
2014-08-20 18:04:39 UTC
Permalink
Post by Stuart Dallas
$directory = dirname(__FILE__).'/../../../media/images/';
/home/rtsadven/public_html/wrightcheck/admin/resources/php/../../../media/images/pic2.jpg
/home/rtsadven/public_html/wrightcheck/media/images/pic2.jpg
Is that the right path?
Bingo - that is the correct path and the image was moved correctly. Thanks Stuart and everyone else who chimed in on this. Great list!!!

Don Wieland
D W D a t a C o n c e p t s
~~~~~~~~~~~~~~~~~~~~~~~~~
***@dwdataconcepts.com
http://www.dwdataconcepts.com
Direct Line - (949) 336-4828
SKYPE - skypename = dwdata

Integrated data solutions to fit your business needs.

Need assistance in dialing in your FileMaker solution? Check out our Developer Support Plan at:
http://www.dwdataconcepts.com/DevSup.php

Appointment 1.0v9 - Powerful Appointment Scheduling for FileMaker Pro 9 or higher
http://www.appointment10.com

For a quick overview -
http://www.appointment10.com/Appt10_Promo/Overview.html
Stuart Dallas
2014-08-20 18:08:22 UTC
Permalink
Post by Stuart Dallas
Post by Stuart Dallas
$directory = dirname(__FILE__).'/../../../media/images/';
However, from that error message you can see the full path that will be
/home/rtsadven/public_html/wrightcheck/admin/resources/php/../../../media/images/pic2.jpg
Post by Stuart Dallas
/home/rtsadven/public_html/wrightcheck/media/images/pic2.jpg
Is that the right path?
Bingo - that is the correct path and the image was moved correctly. Thanks
Stuart and everyone else who chimed in on this. Great list!!!
Good stuff. For future reference it's generally a really bad idea to rely
on assuming what the current directory will be, so you should always base
directories off a known point such as the directory of the current script
[dirname(__FILE__) or __DIR__ in 5.3+] or a globally defined absolute
directory [which should also ideally come from dirname() or __DIR__].

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Aziz Saleh
2014-08-20 17:54:02 UTC
Permalink
Post by Stuart Dallas
Sorry, hit the wrong button and sent before I'd finished.
Post by Don Wieland
Post by Aziz Saleh
If you show us the code it would be easier to help.
Warning: move_uploaded_file(../../../media/images/pic2.jpg): failed to
open stream: No such file or directory in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpKuJ4td'
to '../../../media/images/pic2.jpg' in
/home/rtsadven/public_html/wrightcheck/admin/resources/php/upload_image.php
on line 40
Here is my code - there are a few debugging echoes though - sorry
//Set the values resulting from the Upload form POST.
$tmp_name = $_FILES['file'] ['tmp_name'];
$name = $_FILES['file'] ['name'];
$size = $_FILES['file'] ['size'];
$type = $_FILES['file'] ['type'];
// $error = $_FILES['file'] ['error'];
$extension = strtolower(substr($name, strpos($name, '.') +1));
// If values are set and not empty move_upload_file is invoked to place
the uploaded file into the web server directory identified by $directory.
if (isset($name)) {
// Check for a valid image filetype.
if (!empty($name)) {
// echo 'Filename is not empty.<br><hr>';
if ($extension == 'jpg' || $extension == 'jpeg' ||
$extension == 'png' || $extension == 'gif') {
echo 'Temporary Filename: '.$tmp_name.'<br><hr>';
echo 'Actual Filename: '.$name.'<br><hr>';
$directory = '../../../media/images/';
// $directory = '../../../resources/uploads/'; This
fails also so it does not appear to be a permissions issue as we should be
able to create a file into this project directory.
$destination = $directory.$name;
'.$destination.'<br><hr>';
echo '<a href="'.$destination.'"
target="_blank">link</a><br><hr>';
if (move_uploaded_file($tmp_name, $destination)) {
// This is failing but I cannot figure out why.
I do not get an error message in the error.php file in the server directory
/edit_image/error.php like I get any other time there is a php error.
// if (move_uploaded_file($tmp_name,
'../../../resources/uploads/uploaded.jpg')) {
// echo 'Image file '.$name.' successfully
uploaded to '.$directory.'.';
echo 'Image file successfully uploaded.<br><hr>';
} else {
echo 'move_upload_file failed!<br><hr>';
}
} else {
echo '<span class="formErrorMessage">Image File
Type must be jpg, jpeg, png or gif!</span><br><br>';
}
} else {
echo '<span class="formErrorMessage">Please choose a
file to upload!</span><br><br>';
}
}
I would always recommend using absolute paths rather than relative, mainly
because it helps debugging.
$directory = '../../../media/images/';
$directory = dirname(__FILE__).'../../../media/images/';
And check the path in the error message is correct.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
When dealing with files I usually set a define (or a config) for the file
location in a shared spot between all the code (add public_html if you want
to make the files public):

define('FILE_LOCATION', '/home/rtsadven');
// If you want the files to be public accessed
// define(FILE_LOCATION, ' /home/rtsadven/public');

Then use it in the code:

$directory = FILE_LOCATION . '/media/images/

This will enable you to change the location in the event you want to move
to CDN or a different hard drive easily.
Stuart Dallas
2014-08-20 17:24:29 UTC
Permalink
Post by Don Wieland
Morning folks,
http://admin.wrightcheck.com/admin/resources/php/upload_image.php
http://www.wrightcheck.com/media/images/thefilename.jpg
So far all combination are not working. What is the best way to handle
paths with such a routine? Not sure if there is something in my .httpaccess
config that is not allowing this.
Are you literally trying to move the file to "
http://www.wrightcheck.com/media/images/thefilename.jpg"? If so, that's
your problem. You need to work on the local filesystem only.

If you already are then the problem could be related to safe mode or an
open_basedir setting, which would likely restrict you to working within the
subdomain's root directory.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Don Wieland
2014-08-20 17:30:58 UTC
Permalink
Post by Don Wieland
Morning folks,
http://admin.wrightcheck.com/admin/resources/php/upload_image.php
http://www.wrightcheck.com/media/images/thefilename.jpg
So far all combination are not working. What is the best way to handle paths with such a routine? Not sure if there is something in my .httpaccess config that is not allowing this.
Are you literally trying to move the file to "http://www.wrightcheck.com/media/images/thefilename.jpg"? If so, that's your problem. You need to work on the local filesystem only.
If you already are then the problem could be related to safe mode or an open_basedir setting, which would likely restrict you to working within the subdomain's root directory.
Sorry - I just used the links to show the paths. I guess the absolute local paths are:


wrightcheck/admin/resources/php/upload_image.php (which admin is a sub-domain)

and I want to use the "move_uploaded_file()" function to a folder in the upper main domain:

wrightcheck/media/images/thefilename.jpg (which is the main domain)


Don Wieland
D W D a t a C o n c e p t s
~~~~~~~~~~~~~~~~~~~~~~~~~
***@dwdataconcepts.com
http://www.dwdataconcepts.com
Direct Line - (949) 336-4828
SKYPE - skypename = dwdata

Integrated data solutions to fit your business needs.

Need assistance in dialing in your FileMaker solution? Check out our Developer Support Plan at:
http://www.dwdataconcepts.com/DevSup.php

Appointment 1.0v9 - Powerful Appointment Scheduling for FileMaker Pro 9 or higher
http://www.appointment10.com

For a quick overview -
http://www.appointment10.com/Appt10_Promo/Overview.html
Stuart Dallas
2014-08-20 17:33:31 UTC
Permalink
Post by Stuart Dallas
Post by Don Wieland
Morning folks,
http://admin.wrightcheck.com/admin/resources/php/upload_image.php
and I want to use the "move_uploaded_file()" function to a folder in the
http://www.wrightcheck.com/media/images/thefilename.jpg
So far all combination are not working. What is the best way to handle
paths with such a routine? Not sure if there is something in my .httpaccess
config that is not allowing this.
Are you literally trying to move the file to "
http://www.wrightcheck.com/media/images/thefilename.jpg"? If so, that's
your problem. You need to work on the local filesystem only.
If you already are then the problem could be related to safe mode or an
open_basedir setting, which would likely restrict you to working within the
subdomain's root directory.
Sorry - I just used the links to show the paths. I guess the
wrightcheck/admin/resources/php/upload_image.php (which admin is a sub-domain)
wrightcheck/media/images/thefilename.jpg (which is the main domain)
Do you have error_reporting set to E_ALL and display_errors on? If not, do
both of those and you should get an error that tells you more about what's
happening.

Beyond that, please show your code.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Loading...