uploading file to subdomain on same server-permission denied - php

I'm using this code to upload file to my server. It works just fine if I am uploading to same server but it does not upload if I am uploading to subdomain which is in the same server but different cpanel and everything.
Here is my code:
<?php
$url = 'http://www.indiancinemagallery.com/gallery/vaani-kapoor/Vaani-Kapoor-at-Radio-Mirchi-Stills-(9)9678.jpg';
$img = '/home/path_A/something/test/flower.gif';
file_put_contents($img, file_get_contents($url));
?>
I have given correct path for subdomain.
If I give path of same server from where file is being executed it will upload otherwise it will not upload. The main problem is mysql is there in main server so i want to execute the file from main server and store the photo in subdomain and update the details in main server.
Warning: (flower.gif: failed to open stream: Permission denied in /

This is likely a file permission problem. Your script must have write access to wherever $img points to on the server.
Can you try adding the following at the top of your PHP script to see what, if any, errors/warnings that PHP is throwing?
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
That should tell you if it is failing to write to the file and give you some info.
If after changing permissions, you still cannot write to that location, I imagine it is enforced by your web server management software (Web Host Manager, cPanel, etc.) and how it configures the file space. At that point, you might want to try using PHP's FTP functions (preferably SFTP for security): http://php.net/manual/en/wrappers.ssh2.php

Related

PHP File Upload to remote server

I am currently having problems with testing a known working file upload script while migrating a site to a new server.
I have migrated the entire site to the new server and everything works as it should apart from the original file upload script. I have not pointed the domain names to the new server as yet, accessing it through changing my hosts file on my local machine.
Before writing a simple upload test script, could there be any reason I am missing that is preventing my file uploads from working - for example, security issues?
Is it likely that the file upload directory location is pointing to the original server?
Please, put all the files in the content folder directly to the public directory of the live server.
E.g: Select all in the content folder and paste it directly on the public folder.

Unable to download file using FTP, after upload file via PHP

I have written simple PHP code to upload image file. Images are uploading successfully.
Here is my code,
mkdir("uploaded images", 0777, true);
move_uploaded_file($sourcePath,$targetPathNew);
but when i download that image, it shows me
Response: 550 Access is denied.
Error: Critical file transfer error
Thanks
This is due to permissions of the file. The uploaded file is owned by a web server user (such as www-data) and your FTP server runs under different user. While you change permissions on the folder to 0777 (allow everything to everyone), the file doesn't inherit the same permissions.
To fix this, you probably can add chmod($targetPathNew, 0777) in your code after the move_uploaded_file(...).
There is a chance though that this won't work due to some stricter server configuration. I'm not going to dive into this as judging by your question you're not very familiar with the Linux permissions (sorry if I'm wrong). You can find some essential information about permissions here, for example - https://www.tutorialspoint.com/unix/unix-file-permission.htm

fopen() URL failure

I have 2 servers, the main and the test one. They're on the same network.
The users have files in a shared folder, to which both have access, using a virtual directory with read access.
The PHP.ini are pretty much the same on core and extensions.
I'm using MPDF to generate a PDF, and in the HTML there is a URL to an image in the shared folder. If I echo the HTML, it shows image, if I do $mpdf->Output(); it fails with this fopen() error:
failed to open stream: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection
Both servers have allow_url_fopen but only the test server can generate the PDF well.
It looks like a permission issue. You have 2 users with read access to a virtual directory, and you get an error with $mpdf->Output(); that looks like it may require write access.

php fopen can't find file that definitely exists

I'm trying to load data from a CSV on my Windows PC into a database, something I've successfully done previously. fopen can't find my input file.
Here's the specific code I'm having trouble with:
<?php
ini_set('track_errors', '1');
$handle = fopen("C:/Users/Sam/Documents/test.csv", 'r') or die("can't open file: $php_errormsg");
?>
The error printed is:
[function.fopen]: failed to open stream: No such file or directory
The file definitely exists, and I get the same problem on Unix machines. How do I fix this?
Windows 7 (and Vista?) only lets a user access his own home directory and does not allow Apache (or other users) to. Unfortunately, this is a major headache, and I would suggest that you just move the file somewhere public.
This type of behavior is easier to fix in Linux, but you're still better off moving the file out of your directory into some path where Apache has read access.

not able to upload file on remote server

I am trying to upload image file from my one server to another remote server (owned by me). but its giving me error
Warning: move_uploaded_file(http://www.mysite.org/photo/color-sample-colorize12-10-2010-09-14-09.jpg)
[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections.
Thanks for any suggestion or help in advance
You cant do that.... You need the sites to be on the same physical server and have the directory youre moving to have the proper permissions and be mapped in to both sites.
You could however use ftp or cURL functions to actually upload the file to the remote server, just not move_uploaded_file.
You need to get the file from the remote server using something like file_get_contents and then save it to a file on the local server using file_put_contents, or ftp, or curl if you have the permissions. You can't just copy a file like you would if it was on the same server. (I assume this is what you're trying to do, right?)

Categories