how to get images from another domain - php

function givemeG($path) {
$files = glob($path . '*.jpg');
$cs=" class=\"act\"";
foreach($files as $img) {
echo "<img" . $cs . " src=" . "\"" . $img . "\"" . " alt=\"img\">";
$cs="";
echo "\n";
}
}
$path = "frederic/";
givemeG($path);
This writes img tags for all images inside frederic folder
How can I do the same but instead of frederic folder I need the images from another domain (the same host server).
Simply replacing path doesn't work:
$path = "http://www.codee.site50.net/frederic/";
givemeG($path);

Related

PHP while($content = zip_read()) missing files

Im using script to go through content of zipped files and the script just misses some of them
We have multiple ZIP archives on a server where invoices (Flat files) are stored. Im using following code to search though them:
foreach($zip_files as $zip_file){
echo $zip_file . "[" . $counter . "/" . $count . "]\n";
$counter++;
//Opening and reading zip file
$zip_content = zip_open($archive_path . "\\" . $zip_file);
while($file = zip_read($zip_content)){
$filename = zip_entry_name($file);
if(strpos($filename,".rcv")){
$content = zip_entry_read($file,4086);
if(strpos($content, "<MvxEnvelope>")){
if(strpos($content, "<Field>TETINR</Field>")){
$type = "TETINR";
$TINR_position = strpos($content, "<Field>TETINR");
$DIVI_position = strpos($content, "<Field>DIVI");
$TINR = substr($content,($TINR_position + 28), 10);
$DIVI = substr($content,($DIVI_position + 26), 3);
fwrite($map, $zip_file . ";" . $filename . ";" . $DIVI . ";" . $TINR . ";" . $type . "\n");
}
}
}
}
}
I would like the script to go through all files and not to miss some of them. Many thanks in advance

move_uploaded_file and the copy

I am trying to upload a tmp file and then copy it. But it is not appearing in the second folder, is there an overlay or something I have to watch?
$uploadPath = '/.../..../image/';
$uploadPathpreview = '/.../..../blog/';
if ($counter == 0){
$preview_file = $uploadPath . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
$preview_file_save = $uploadPathpreview . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
if(move_uploaded_file($tmp_name, $preview_file)){
$tmp = $preview_file;
$new = $preview_file_save;
}
copy($tmp,$new);
The file in image exists by the way. And I tried it without the if()

Can't add all files in the zip (php)

So I found this class on the internet that can put files into a zip.
I have a form where I can upload pictures into a folder, but when I try to add them into the zip I could add only the last file from that folder (loop).
zipper class
<?php
class zipper {
private $_files = array(),
$_zip;
public function __construct() {
$this->_zip = new ZipArchive;
}
public function add($input){
if(is_array($input)){
$this->_files = array_merge($this->_files, $input);
}else{
$this->_files[] = $input;
}
}
public function store($location = null){
if(count($this->_files) && $location){
foreach ($this->_files as $index => $file) {
if(!file_exists($file)){
unset($this->_files[$index]);
}
print_r($file . "<br>");// here gives me the exact path of the files.
}
if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach ($this->_files as $file) {
$this->_zip->addFile($file, $file);
}
$this->_zip->close();
}
}
}
}
and this is my uploading file
> $uniqUser = uniqid(strtoupper($userName). "-" , true);
> $directory ='../upload/';
> $file_name is $files['name']
if(!is_dir("../upload/". $uniqUser ."/")) {
mkdir("../upload/". $uniqUser ."/");
}
if(move_uploaded_file($file_tmp, $directory . $uniqUser . "/" . $file_name)){
$uploaded[$position] = $directory . $uniqUser . "/" . $file_name;
$zipper = new zipper;
$zipper->add(BASE_URL . "/upload/" . $uniqUser . "/" . $file_name);
$zipper->store(BASE_URL . "/upload/" . $uniqUser . ".zip");
the last code is inside a foreach that loops into the files uploaded.
When I did a print_r("Added file:" . $file . "<br>"); to see if the all files are added, I get a positive response. But I always get the last file from the folder.
Thank you
EDIT
Well, Guess the problem is after each loop to add the files in the folder, the zip class adds the file into the zip, then after checks if the zip exists or no ... So after each loop the zip is overwritten and adds only the last file.
How can I make this better ?
The problem was kinda stupid ...
I had to create the class outside the foreach $zipper = new zipper; and $zipper-store("../upload/" . $uniqUser . ".zip");for unnecessary repeating (added it at the bottom).

Stored image in database doesn't show on page

Image is stored in database (as varchar(255)), but on page isn't shown.
This is code that stored image, and put's image in folder:
if (isset($_FILES["image"])) {
$title = date("dmyHms") . "_" . $_FILES["image"]["name"];
$path = "img/profile/" . $_POST["id"] . "_" . $title;
move_uploaded_file($_FILES["image"]["tmp_name"], $path);
}
And this is to display image:
foreach ($conn->results() as $conn):
$img = $_SERVER["CONTEXT_DOCUMENT_ROOT"] . $path . "img/profile" . $conn->id . "_" . $conn->image;
if (file_exists($img)) {
$image = $path . "img/profile/" . $conn->id . "_" . $conn->image;
} else {
$image = $path . "img/noimage.png";
}
<?php endforeach; ?>
When I print_r $img, it shows right path to image and still image is not displayed.
Display:
<?php echo $image; ?>
Where did you echoing your image? If it is inside foreach, you need to change your code little bit...
$image = '<img src="'.$path . "img/profile/" . $conn->id . "_" . $conn->image . '" />';
Hope, will work perfectly for you.... TQ
The following line is not correct:
foreach ($conn->results() as $conn):
Don't overwrite $conn. Change the variable to something different like $result

Copy files from one DV server to another PHP

But what's the best and easiest way to copy a file or folder between a local and remote server using php? These are files located above the web folder, so I'll need to use paths instead of the URL.
I would do it using PHP's built-in FTP functions.
EDIT: Ahh, you want secure. This is what I would use then: SSH2-SFTP
Well i made this function hope it works for you copy files from ftp:
$ftpConnection = the conection, example ftp_connect(1.0.0.1).
$path = the ftp path.
$destination = the local file.
function ftpRecursiveFileListing($ftpConnection, $path, $destination) {
$contents = ftp_nlist($ftpConnection, $path);
foreach ($contents as $currentFile) {
if (strpos($currentFile, '.') === false) {
$dir = basename($currentFile);
echo "<br> <b> Directorio </b>" . $dir;
mkdir($destination . "/" . $dir);
ftpRecursiveFileListing($ftpConnection, $currentFile, $destination . "/" . $dir);
} else {
$file = basename($currentFile);
echo '<br> <b>archivo </b>' . $file;
echo '<br> <b>path </b>' . $path;
echo '<br> <b>completo </b>' . $path . "/" . $file;
ftp_get($ftpConnection, $destination . '/' . $file, $path . '/' . $file, FTP_BINARY);
}
}
}

Categories