PHP copy() failed to open stream: Invalid argument - php

I am trying to upload a file from my brower->PHP script->Server.
My browser and PHP script lie on the same server. I am using xampp on this. The server where I have to upload this file is different.
Here is my code:
$fdir = "http:\\\\myip\\D:\\errors\\";
$ffile = $fdir.basename($_FILES['myfile']['name']);
if (copy($ffile,$_FILES['myfile']['tmp_name'])) {
echo "<br />"."File uploaded successfully";
} else {
echo "<br />"."Error in uploading file";
}
I am getting the following error:
copy(http:\myip\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54
Any help would be appreciated.
EDIT:
I edited my code to remove http:// and use the name of the server.
Also I am now using move_uploaded_file instead of copy
$fdir="\\\\TESTSRV\\D:\\errors\\";
$ffile=$fdir.basename($_FILES['myfile']['name']);
move_Uploaded_file($_FILES['myfile']['tmp_name'],$ffile);
But Still it gives me the error
move_uploaded_file(\TESTSRV\D:\errors\IMG-20150424-WA0004.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\BS\myphp1.php on line 54

This is not the way to copy uploaded files - you need to use the function move_uploaded_file:
if (move_uploaded_file($_FILES['myfile']['tmp_name'], "c:\\path\\to\\file")) {
echo "<br />"."File uploaded successfully";
} else {
echo "<br />"."Error in uploading file";
}
Also I don't think your $fdir variable is a valid path in Windows. Basically the second argument of move_uploaded_file should be the target path, where you'd like to move the file.

Related

Cannot use absolute path to a file received via STDIN in PHP CLI

As a part of some automation scripts I'm writing a function that asks the user for an absolute path to the file she wishes to use. The problem is that PHP throws Warning: fopen("/path/to/desired/file/file.txt"): failed to open stream: No such file or directory in /path/to/my/script/script.php on line 13 when the user inserts the file. Here is the general function:
function askFile() {
echo "Please specify the full path to your file.\n";
$usrInput = trim(fgets(STDIN, 1024));
$usrInput = '"' . $usrInput . '"';
echo $usrInput . "\n"; // Just to test the final string.
echo is_string($usrInput) ? "It's a string.\n" : "It's not a string.\n"; // Just to confirm that the variable holds a string.
if($handle = fopen($usrInput, 'r')) {
echo "Thank you. The file is now opened.\n";
} else {
"Sorry, could not open the file for reading.\n";
}
}
askFile();
The permissions for the file are, for testing purposes, wide open at 777. When I simply hard code the absolute path right inside fopen() everything works as expected, therefore the problem only occurs when the input comes from STDIN.

How to Copy File from Another Server Using PHP?

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.

PHP - Upload file error - Permission denied

i am trying to run this simple php code:
<?php
$dir = getcwd()."\uploads\ ";
if($_FILES['myfile']['error'] != 0)
{
echo "Error uploading the file: {$_FILES['myfile']['error']}";
}
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $dir . $_FILES['myfile']['name'])) {
echo 'Success!!!';
} else {
echo 'Error.';
}
?>
I am getting this problem:
failed to open stream: Permission denied in... on line 7 Unable to
move ... to ... on line 7
Line 7 is: move_uploaded_file
I understand that maybe the problem is with the folder permission, and i need to use CHMODE command.
But i don't understand how to use it and where do i put it in my code.
Use slashes /instead of backslashes. And check permissions to target dir.
$dir = getcwd() . "/uploads/";
Well, you can call chmod() before the line with move_uploaded_file() for example like this
chmod($dir, 755);
You can find more information about chmod() on the documentation sites

PHP File Upload Error Line 17

I wrote a script that would allow me to upload images and files to my servers, and now that I've switched domains, everything seems a bit messed up.
I've changed the urls and directories, and I've got my chmod set at 777 for the directories needed (cdn and img)
Script:
$folder = "/cdn/img";
$HTTP_POST_FILES = "";
if(isset($_FILES['filename']['tmp_name'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
$ext = strtolower(end(explode('.', $_FILES['filename']['name'])));
$fileCode = fileCode($ext);
if(move_uploaded_file($_FILES['filename']['tmp_name'], $folder . $fileCode)) {
echo 'Your file has been uploaded! View and share your file here';
} else {
echo "THERE'S A GLITCH IN THE MATRIX! YOUR FILE COULDN'T BE UPLOADED!";
}
} else {
Echo "Failed. Try again.";
}
And I'm getting this error:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpZ1TbaL' to 'http://codyleek.me/cdn/img/e6fmd6.png' in /home/codyleek/public_html/cdn/upload.php on line 17
Sorry that my formatting sucks. I'm new here aha.
But um, could any of you help me? I've tried redoing the URLs, resetting permissions, everything I can think of.
My PHP knowledge is limited.
Thanks in advance.
You should move it to a path on your disk (for example /srv/www/mydomain.com/img/test.png) and not to another website like you're doing now (http://...). By using http://x you are saying: 'use the HTTP-protocol, on domain x to ....'

In PHP, check if a file can be deleted

I have a large video file that is streamed using a streaming server outside my control, and sometimes I want to delete the video file. When the file happens to be viewed by someone using the streaming server, PHP errors with "Permission denied".
I would like to check before attempting to delete if the file can be deleted or not. I do not want to actually try to delete the file and see if that fails, I would like to check beforehand.
This is my code so far:
$file = "video.flv";
$file2 = "newvideoname.flv";
clearstatcache();
if (is_writeable($file)) {
echo "is writeable";
}
else {
echo "is NOT writeable";
}
echo "\n";
$fh = fopen($file, 'a+');
if (!flock($fh, LOCK_EX | LOCK_NB)) {
// file locked, do something else
echo "is locked";
}
else {
echo "not locked!";
}
fclose($fh);
echo "\n";
if (touch($file)) {
echo "modification time has been changed to present time";
}
else {
echo "Sorry, could not change modification time";
}
echo "\n";
rename($file, $file2);
The output I get when I stream video.flv while executing the code:
is writeable
not locked!
modification time has been changed to present time
PHP Warning: rename(video.flv,newvideoname.flv): Permission denied in ...
Sometimes I get:
is writeable
PHP Warning: fopen(video.flv): failed to open stream: Permission denied ...
PHP Warning: flock() expects parameter 1 to be resource, boolean given
is locked
PHP Warning: fclose(): supplied argument is not a valid stream resource
PHP Warning: touch(): Utime failed: Permission denied
Sorry, could not change modification time
PHP Warning: rename(video.flv,newvideoname.flv): Permission denied ...
So sometimes the file cannot be locked by PHP and it cannot be touch()ed by PHP, and then of course the rename doesn't work, but sometimes PHP says "all is fine" until the rename command. The rename command has never worked by random chance.
What should I do with the file?
if (!flock(fopen($file, 'a+'), LOCK_EX | LOCK_NB)) {
You are locking the file. Make sure you unlock it again if the call succeeds (just after echo "not locked!";
The problem as I see it is that when you checked if the file is locked with flock you opened a resource to the file but did not close the file. So the file is now locked and you can't rename it.
You cannot delete a file that is streaming...if you REALLY WANT TO...make a copy, raname it, and put the file(original) in a stack(DB) and have a cron job or something to delete it...
The solution is here :
$file = "test.pdf";
if (!is_file($file)) {
print "File doesn't exist.";
} else {
$fh = #fopen($file, "r+");
if ($fh) {
print "File is not opened and seems able to be deleted.";
fclose($fh);
} else {
print "File seems to be opened somewhere and can't be deleted.";
}
}

Categories