I'm building a module installer for my CMS and i have ran into a problem now im using Windows 7 x64 with XAMPP x32 installed,
For some reason PHP returns the ER_OPEN error code (11) when ever i run my code and i know the directoy is writeable as the file gets move to the parent by php when it's uploaded here is my script
if($_URL['form'] == 'sent'){
$target_path = getcwd()."\\..\\Temp-uplds\\";
$target_path = $target_path. time() . basename( $_FILES['installFile']['name']);
if(move_uploaded_file($_FILES['installFile']['tmp_name'], $target_path)) {
$zip = new ZipArchive;
$status = $zip->open('$target_path');
if ($status === TRUE) {
$zip->extractTo(getcwd()."\\..\\Temp-uplds\\zip\\");
$zip->close();
} else {
print_r(array($status, $zip));
}
} else{
$this->tmp_vars->error = true;
}
}
echo "<pre>error codes ZIPARCHIVE::ER_EXISTS = '".ZIPARCHIVE::ER_EXISTS."'
File already exists.
ZIPARCHIVE::ER_INCONS = '".ZIPARCHIVE::ER_INCONS."'
Zip archive inconsistent.
ZIPARCHIVE::ER_INVAL = '".ZIPARCHIVE::ER_INVAL."'
Invalid argument.
ZIPARCHIVE::ER_MEMORY = '".ZIPARCHIVE::ER_MEMORY."'
Malloc failure.
ZIPARCHIVE::ER_NOENT = '".ZIPARCHIVE::ER_NOENT."'
No such file.
ZIPARCHIVE::ER_NOZIP = '".ZIPARCHIVE::ER_NOZIP."'
Not a zip archive.
ZIPARCHIVE::ER_OPEN = '".ZIPARCHIVE::ER_OPEN."'
Can't open file.
ZIPARCHIVE::ER_READ = '".ZIPARCHIVE::ER_READ."'
Read error.
ZIPARCHIVE::ER_SEEK = '".ZIPARCHIVE::ER_SEEK."' </pre>";
And my Given output is
Array ( [0] => 11 [1] => ZipArchive Object ( [status] => 0 [statusSys] => 0 [numFiles] => 0 [filename] => [comment] => ) )
Any help would be helpfull
You have single quotes around '$target_path'. Variable interpolation only works with double quotes.
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Related
I am currently uploading files to a third party server through SFTP by using PHP ssh2
The connection are working fine and I tried to upload files using fwrite
$resourceId = $this->sftp;
$intResourceId = intval($resourceId);
$dir = "ssh2.sftp://$intResourceId/./".$this->un."/".$file_name;
$stream = #fopen($dir, 'w');
if (!$stream) {
return "Could not open file: $file_name";
}
$data_to_send = #file_get_contents(LOCAL_FILE_DIR.$file_name,true);
if ($data_to_send === false){
return "Could not open local file: $file_name.";
}
if (#fwrite($stream, $data_to_send) === false){
return "Could not send data from file: $file_name.";
}
/*
readdir():
Array
(
[0] => 1810171932.txt
[1] => response_dir
)
*/
The files were uploaded when I read the directory but after a few seconds it was removed from the directory.
/*
readdir():
Array
(
[0] => response_dir
)
*/
Any idea why? Does the file being uploaded or there are some permission issue which I need to take care of?
Thanks in advance.
I have some videos,images and text files in "uploads/video/" dir.
Here i am getting all the files using scandir but I want only videos from that folder.
Sample code :
$video_dir = 'uploads/video/';
$video_array = scandir($video_dir);
unset($video_array[0]);
unset($video_array[1]);
echo "<pre>";
print_r($video_array);
echo "</pre>";
Getting Result :
Array ( [2] => ADD.mp4 [3] => COO_Notes.txt [4] => Carefree.mp3 [5] => Circus Tent.mp3 [6] => Phen.mp4 [7] => REM.mp4 [8] => images (9).jpg [9] => images.jpg [10] => test.php )
I need only video files. Remove the text,mp3,jpg,etc files:
Array ( [2] => ADD.mp4 [6] => Phen.mp4 [7] => REM.mp4)
Thanks for your updates.
You can use glob():
<?php
foreach (glob("*.mp4") as $filename) {
echo "$filename - Size: " . filesize($filename) . "\n";
}
# or:
$video_array = glob("*.mp4");
?>
In order to get multiple formats, simply put the extensions in curly braces and add the parameter GLOB_BRACE:
$video_array = glob('uploads/video/{*.mp4,*.flv,*.mov}', GLOB_BRACE);
See it on PHP.net here.
I believe the function pathinfo should help you out.
http://php.net/manual/en/function.pathinfo.php
<?php
$videos = array();
$video_ext = array('mp4', 'mpeg');
foreach ($video_array as $path) {
if (in_array(pathinfo($path, PATHINFO_EXTENSION), $video_ext)) {
//less general, but will work if you know videos always end in mp4
//if (pathinfo($path, PATHINFO_EXTENSION) == "mp4") {
$videos[] = $path;
}
}
You may check file type with mime_content_type: http://php.net/manual/en/function.mime-content-type.php.
Assume this will be something like:
$videos = array();
$dir = 'uploads';
$files = scandir($dir);
foreach($files as $file) {
$filepath = $dir . '/' . $file;
if(is_file($filepath)) {
$contentType = mime_content_type($filepath);
if(stripos($contentType, 'video') !== false) {
$videos[] = $file;
}
}
}
Also this may be not very fast and perhaps will not detect all possible (strange) video files (as it uses magic.mime). But this may work without array of file extensions and also will look at file itself rather than filename.
So far after looking of multiple examples of how to unzip a file i'm a little confused on what i'm missing for this to work now.
Im using WordPress and AdvancedCustomFields to designate what kind of file i'm going to be uploading. I need to unzip this file and figure out the internal files to use one as a source.
} else if(get_sub_field('media_type') == 'Zip'){
/* Get Path Name */
$file = get_sub_field('file');
$pieces = explode("/", $file);
$lastZip = end($pieces);
array_pop($pieces ); //removes last
$path = implode("/", $pieces);
$path = $path."/";
/* Get Name of File and concat .png */
$last = explode(".", $lastZip);
$last = $last[0].".png";
/* Append new filename to proper pathing */
$pathFile = $path.$last;
$zip = new ZipArchive;
$zip->open($lastZip, ZipArchive::CREATE);
print_r($zip);
if ($zip === TRUE) {
$zip->extractTo($path);
$zip->close();
echo 'File extracted to: $path';
} else {
echo "does not work!";
}
?><span><img src="<?php echo $pathFile; ?>" /></span>
<?php
}
My outcome of print_r($zip) is:
ZipArchive Object (
[status] => 0
[statusSys] => 0
[numFiles] => 0
[filename] => /var/www/vhosts/domain.com/httpdocs/example.zip
[comment] =>
)
I'm trying to download a file from my website to my server but can't find why or where I'm doing that wrong.
Here my php code :
$fn = $_FILES['file']['name'];
if (is_writable('.')) {
echo "Writable<BR>";
} else {
echo "Not writable<BR>";
}
$upfile = './'.basename($fn);
echo $upfile.'<BR>';
shell_exec("echo 'baaaaah' > test.baaaah");
$f = $_FILES['file']['tmp_name'];
echo $f.'<BR>';
if (is_uploaded_file($f)) { echo "uploaded<BR>"; } else { echo "not uploaded<BR>";}
$com = "test - f ".$f." && echo 'F' || echo 'N'";
echo $com.'<BR>';
echo shell_exec($com).'<BR>';
if (move_uploaded_file($f,$uploadfile)) {
echo "File transfer OK<BR>";
} else {
echo "File transfer NOK<BR>";
}
print_r($_FILES);
And here the website output :
Writable
./flag.jpg
/tmp/phpyKvhEz
uploaded
test - f /tmp/phpyKvhEz && echo 'F' || echo 'N'
N
File transfer NOK
Array ( [file] => Array ( [name] => flag.jpg [type] => image/jpeg [tmp_name] => /tmp/phpyKvhEz [error] => 0 [size] => 1660 ) )
So first thing I check if my folder is writable (it's not intended to be '.' but I moved to here because the folder I want wasn't working either (same behavior as '.' through...)) => check
Then I try to shell_exec a file here juste to be sure => check, file is on server
Then I check if the temp file is created on the server :
- check, anyway is_uploaded thinks the file is here
- not check, but a test on the temp file doesn't work (and since the file is not supposed to be removed before the end of the script it should see the file here imho ?)
Then I try to make the move_uploaded_file => not check
And I print the $_FILES who shows nothing suspicious (error = 0, file names matches what I see before).
I can't figure what goes wrong nor where a mistake can come from ><
$uploadfile is undefined in your code. Change it or set it to '.' and it should work.
As a bit of a follow up to Javascript form won't submit (to view the code I am using visit that link) I am now encountering a problem that I cannot find the file that has been uploaded.
I have added $files = apc_fetch('files_'.$_POST['APC_UPLOAD_PROGRESS']); to the top of my page and this is the output of print_r($files);
Array
(
[theFile] => Array
(
[name] => tt1.mp4
[type] => video/mp4
[tmp_name] => /tmp/php2BEvy7
[error] => 0
[size] => 1050290
)
)
However when I try to run the following code:
if (file_exists($files['theFile']['tmp_name'])) {
$webinarType = strcmp($files['theFile']['type'], 'video/mp4');
if($webinarType == 0) {
$webinarFile = $fileTitle;
$webinarTempName = $files['theFile']['tmp_name'];
} else {
echo 'Webinar must be .mp4';
}
} else {
echo "No File";
}
I get the No File output.
I have ssh'd into the server and the file is not in /tmp/, /path/to/public_html/tmp/ or path/to/file/tmp/ all of which exist.
I have tried to use move_uploaded_file() but as this is executed on all file inputs I can't get the tmp_name dynamically due to my limited knowledge of javascript.
tl;dr version; Where is my file gone and how can I find it?
NOTE; This form did work before the APC intevention and I am running wordpress in case that affects anything.
Fixed this one on my own as well.
In the progress.php file (found on the other question) I modified the elseif statement with this:
elseif(($s_progressId = $_POST['APC_UPLOAD_PROGRESS']) || ($s_progressId = $_GET['APC_UPLOAD_PROGRESS']))
{
// If the file has finished uploading add content to APC cache
$realpath = realpath($PHP_SELF);
$uploaddir = $realpath . '/tmp/';
foreach ($_FILES as $file) {
if(!empty($file['name'])) {
$uploaded_file = $file['name'];
$moveme = $uploaddir.$uploaded_file;
move_uploaded_file($file['tmp_name'], $moveme);
}
}
apc_store('files_'.$s_progressId, $_FILES);
die();
}
That way I could iterate through the $_FILES array without knowing the name of the input. I noticed that it loops through a couple of times hence the if(!empty()) however in hindsight it's probably best practice anyway.