Re: Writing a file from a Sub-Domain form to Upper Domain
| From: | Aziz Saleh | Date: | Wed, 20 Aug 2014 17:54:02 +0000 |
| Subject: | Re: Writing a file from a Sub-Domain form to Upper Domain | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323763@lists.php.net to get a copy of this message | ||
On Wed, Aug 20, 2014 at 1:49 PM, Stuart Dallas <stuart@3ft9.com> wrote:
> Sorry, hit the wrong button and sent before I'd finished.
>
> On 20 August 2014 18:47, Stuart Dallas <stuart@3ft9.com> wrote:
>
>>
>> On 20 August 2014 18:43, Don Wieland <donw@dwdataconcepts.com> wrote:
>>
>>> On Aug 20, 2014, at 10:19 AM, Aziz Saleh wrote:
>>>
>>> > 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>';
>>> }
>>> }
>>>
>>
> 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/
>
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.