This question already has answers here:
failed to open stream: HTTP wrapper does not support writeable connections
(4 answers)
Closed 7 years ago.
SO I am getting this error and searched everywhere for a reason. From what I have read this code should work. It is not using a url to write the file but instead a relative path. All the file permissions are set up right to 755 so dont think that is the issue. I even had a plugin developer looked at it and he didnt see an issue. Anyone got any ideas why this error is happening?
Error:
`Warning: move_uploaded_file(http://************/wp-content/uploads/simpleecommcart/digitalproduct/13-1.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/***********/public_html/wp-content/plugins/simple-e-commerce-shopping-cart/models/SimpleEcommCartProduct.php on line 880`
`Warning: move_uploaded_file(): Unable to move '/tmp/phpyv2ztG' to 'http://***********/wp-content/uploads/simpleecommcart/digitalproduct/13-1.jpg' in /home/stagingpeak/public_html/wp-content/plugins/simple-e-commerce-shopping-cart/models/SimpleEcommCartProduct.php on line 880`
The upload code (I wrote 880 where the error was):
//check for product image upload
if(strlen($_FILES['product']['tmp_name']['image_upload']) > 2) {
$dir = SimpleEcommCartSetting::getValue('product_folder');
if($dir) {
$filename = preg_replace('/\s/', '_', $_FILES['product']['name']['image_upload']);
$path = $dir . DIRECTORY_SEPARATOR . $filename;
$src = $_FILES['product']['tmp_name']['image_upload'];
880--> if(move_uploaded_file($src, $path)) {
$_POST['product']['product_image_path'] = $filename;
}
else {
$this->addError('Product Image File Upload', __("Unable to upload file","simpleecommcart"));
$msg = "Could not upload file from $src to $path\n". print_r($_FILES, true);
throw new SimpleEcommCartException($msg, 66101);
}
}
}
The HTTP wrapper doesn't allow writing (for good reason) so this won't work in the way you were hoping, you'll need a stream that allows writing such as:
(Preferably) File: http://php.net/manual/en/wrappers.file.php
FTP: http://php.net/manual/en/wrappers.ftp.php
Source: http://php.net/manual/en/wrappers.http.php
Related
I am using php ftp_connect() and ftp_put() to upload a file from the webserver to an external server.
The code works great when uploading to one directory (in this case /global/1/) but doesn't work when I upload to another one (in this case /{FCADC481-CACAA556A47A}/_schedules/). Therefore I don't think the issue is the code.
The file I am trying to upload is an XML, but I've also tried with .mov and .avi
I have tried both FTP_ASCII and FTP_BINARY
I have tried with and without ftp_pasv
Both directories and sub directories have the same file access attributes (666)
I can upload to both of these folders using Filezilla directly
The error coming back is:
"Core: Error handler (BE): PHP Warning: ftp_put(): Connection closed; Stream-Lesefehler."
$response = false;
$local_file = "/test.xml";
$remote_path = "/{".$pov."}/_schedules/"; // NOTE { and } ARE part of the folder name.
$remote_file = $remote_path . "test.xml";
if($ftp_conn = ftp_connect($this->ftphost, $this->ftpport,15)){
if (ftp_login($ftp_conn, $this->ftpusr, $this->ftppss)) {
ftp_pasv($ftp_conn, true);
if (ftp_put($ftp_conn, $remote_file, $local_file, FTP_ASCII)) {
$response = true;
}
}
}
ftp_close($ftp_conn);
return $response;
The only thing I can think is that ftp via PHP doesn't like folders with a "{" symbol. But I have no control over that server, so I have to use it.
I know { can be used in PHP to dechiper a variable value within a string so I tried swapping the symbol for another one and then str_replacing it again afterwards, but it didn'T solve the problem.
Can anybody help?
help me to solve the problem .
I have wrote my code like this :
function copy(){
$folder = "PDF";
$file = "2016-01-001_01-00.pdf";
$files = "ftp://10.242.42.154/adk_rkakl2016s"." ".substr($file,8,3)."/".substr($file,12,2)."/".$folder."/"."2016-01-001_01-00.pdf";
$newfile = 'files/backup_file/2017-01-001_01-00.pdf';
if (!copy($files, $newfile)) {
echo "failed to copy $files...\n";
}else {
echo "copied $file into $newfile\n";
}
}
i want to copy file from another server, but the result like this :
Severity: Warning Message: copy(): connect() failed: Connection refused Filename: controllers/Download_adk.php Line Number: 146
please help me
Since it is saying connection refused,
Please check whether you are able to view the PDF in browser.
If you can view pdf that is on the same machine as your program, then it might be that the server won't send the pdf unless you are a real user. In that case, modifying the browser identification string might fix your problem.
If you cannot view the pdf from your browser, then I guess, we have to check for some other problem.
This question already has answers here:
PHP - Failed to open stream : No such file or directory
(10 answers)
Closed 6 years ago.
I am creating a profile picture upload function for my site, but for some reason, move_uploaded_file() does not want to function.
I have tried multiple forums and tested out all the possible different approaches, but with no luck.
Here is my PHP & HTML:
<?php
if(isset($_FILES['avatar'])){
if(empty($_FILES['avatar']['name'])){
$errors[] = 'Please select an avatar.';
} else {
$ext = array('jpg', 'jpeg', 'png');
$file = $_FILES['avatar']['name'];
$fileext = strtolower(end(explode('.', $file)));
$filetmp = $_FILES['avatar']['tmp_name'];
if(in_array($fileext, $ext)){
$file = md5(microtime() . $filetmp) . '.' . $fileext;
$filepth = './data/user_data/img/udid/prof/' . $file;
move_uploaded_file($filetmp, $filepth);
} else {
$errors[] = 'Please select a valid file type. (JPG, JPEG or PNG)';
}
}
}
?>
<form action="" method="post" class="avatar-form-form" enctype="multipart/form-data">
<input type="file" name="avatar"><br>
<input type="submit" value="Upload">
</form>
So first off, I am checking that the selected file has a valid extension, if so, it will hash the file temporary name with the microtime (for security). I am then concatenating the extension onto the end with a full stop between it, i.e the output will be md5hash.png.
I am then creating a file path variable by concatenating the file variable onto the end of my directory.
I then proceed with the file upload function by passing through the $filetmp and $filepth variables (like you're supposed to do).
However, when I go to test this function out on my page, I get these errors:
Warning: move_uploaded_file(./data/user_data/img/udid/prof/e4d0cde3c9330222ef3ab651fe797bed.jpg):
failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/settings.php
on line 40
Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpkWSGLy'
to './data/user_data/img/udid/prof/e4d0cde3c9330222ef3ab651fe797bed.jpg'
in /Applications/XAMPP/xamppfiles/htdocs/test/settings.php
on line 40
This is my current layout:
The settings page (the one where the user uploads there picture) is in the root directory.
The location in which I want to put the avatar is inside of a folder (also in the root directory.
I am currently testing all of this on my MacBook running XAMPP and have made sure that file_uploads is set to "on" in my php.ini file.
All help is appreciated. Not sure If I have done this incorrectly, but I am almost certain that I haven't.
EDIT:
So, It turns out, that by default on a MacBook (when running XAMPP), all files inside of XAMPP/htdocs are set to read only and you must manually set them to "read & write" to allow move_uploaded_file to work.
Check that you have set the proper write permissions for your uploaded images folder in your MacBook.
I was following a guide for creating a new file in php and I was using the following code:
function create($user, $name) {
/* Later on, this will connect to another server*/
$dir = $user->getFolder() . "/Projects/". $name;
if(file_exists($dir)) {
$this->error = "Directory: " . $dir . " already exists.";
} else {
mkdir($dir);
//Create the users.json file and add the owner
$json = fopen($dir . "/Data/users.txt", "w") or die("Cannot open file");
fclose($json);
}
}
The directory gets created but I receive the following error: "Warning: fopen(Jake/UserFolder//Projects/test/Data/users.txt): failed to open stream: No such file or directory in D:\xampp\htdocs\Collabs\Objects\Scripts\Project.php on line 14
Cannot open file"
The path shown in your error looks like it could be the problem.
Jake/UserFolder//Projects/test/Data/users.txt
There are two slashes between UserFolder and Projects. It looks like changing your code to
$dir = $user->getFolder() . "Projects/". $name;
Would get rid of the extra slash.
A couple of things that might have gone wrong:
You didn't pass path with a leading /, so fopen() will search relatively to the directory the script is executing in. I assume you'll want to pass an absolute path
you are testing for file_exists(), you should also check for is_dir(), to catch and handle the cases where a file with the same path already exists
you might consider calling mkdir($dir, 077, true) in order to create
the intermediary directories (see the mkdir() documentation)
I have a very basic, very straightforward function that takes a file (after checking it to make sure its a zip among other things) and uploads it, unpacks it and such:
public function theme(array $file){
global $wp_filesystem;
if(is_dir($wp_filesystem->wp_content_dir() . "/themes/Aisis-Framework/custom/theme/")){
$target_path = $wp_filesystem->wp_content_dir() . "/themes/Aisis-Framework/custom/theme/";
if(move_uploaded_file($file['tmp_name'], $target_path . '/' . $file['name'])) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($target_path); // change this to the correct site path
$zip->close();
//unlink($target_path);
}
$this->success('We have uplaoded your new theme! Activate it bellow!');
} else {
$this->error('Oops!', 'Either your zip is corrupted, could not be unpacked or failed to be uploaded.
Please try again.');
}
}else{
$this->error('Missing Directory', 'The Directory theme under custom in Aisis Theme does not exist.');
}
if(count(self::$_errors) > 0){
update_option('show_errors', 'true');
}
if(count(self::$_messages) > 0){
update_option('show_success', 'true');
}
}
Extremely basic, yes I have used my target path as both the path to upload too and unpack (should I use a different path, by default it seems to use /tmp/tmp_name)
Note: $file is the array of $_FILES['some_file'];
My question is I get:
Warning: move_uploaded_file(/var/www/wordpress/wp-content//themes/Aisis-Framework/custom/theme//newtheme.zip): failed to open stream: Permission denied in /var/www/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/FileHandling/Upload/Upload.php on line 82
Warning: move_uploaded_file(): Unable to move '/tmp/phpfwechz' to '/var/www/wordpress/wp-content//themes/Aisis-Framework/custom/theme//newtheme.zip' in /var/www/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/FileHandling/Upload/Upload.php on line 82
Which basically means that "oh the folder your trying to move from is owned by root, no you cannot do that." the folder I am moving too is owned by apache, www-data. - I have full read/write/execute (it's localhost).
So question time:
Should my upload to and move to folder be different?
How, in a live environment, because this is a WordPress theme, will users who have the ability to upload files be able to get around this "you dont have permission"?
You should try to do it the wordpress way. Uploading all user content to wp-content/uploads, and doing it with the native functions.
As you mention, uploading to a custom directory, may be an issue to non tech savvy users. /uploads already have the special permissions.
Check out wp_handle_upload. You just have to limit the mime type of the file.
The path you are trying to upload is some where wrong here
wp-content//themes
try removing one slash