i am uploading the image into the server , need to place the _ in place of gap in the image. Like if the name of image is Stack Flow.jpg, i need to send it as Stack_Flow.jpg in the directory as well in the email. HOw could be possible with the following code. i have tried but no success.. I am sending the 4 files in one form, code as ---
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.$fileb;
if(!empty($fileb)) move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.$filec;
if(!empty($filec)) move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.$filed;
if(!empty($filed)) move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
i am using below function, how could i apply it for all four files--
<script>
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
</script>
here is a quick example in php:
<?php
$name = "Stack Flow.jpg";
echo preg_replace('/[\s\-]+/', '_', $name );
?>
returns Stack_Flow.jpg
http://codepad.org/MQoEZ2wv
This is not a script but PHP..
<?
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
?>
//do the same for all other images..
$filea = str_replace(' ', '_', $filea;
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
Using:
<?php
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
?>
And then your code:
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
if(!empty($filea))
move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.convertSpecialChars($fileb);
if(!empty($fileb))
move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.convertSpecialChars($filec);
if(!empty($filec))
move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.convertSpecialChars($filed);
if(!empty($filed))
move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
Or if possible, you could do it in a loop (less duplicate code):
for ($i = 1; $i <= 4; $i++)
{
$file = $_FILES['FILE' . $i]['name'];
$order_image = 'order_' . $orderId . '_' . convertSpecialChars($file);
if(!empty($file))
move_uploaded_file($_FILES['FILE' . $i]['tmp_name'], "../files/$order_image");
}
In your code change $order_image_a='order_'.$orderId.'_'.$filea; and other similar lines to
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
But will better if you will know how work your code.
Related
I really need some help with this... i just cant make it work.
For now i have this piece of code and it's working fine.
What it does is... retuns all files within a directory according to date in their name.
<?php
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$filteredImages = [];
foreach($images as $image) {
$current_date = date("Ymd");
$file_date = substr($image, 0, 8);
if (strcmp($current_date, $file_date)>=0)
$filteredImages[] = $image;
}
echo json_encode($filteredImages, JSON_UNESCAPED_UNICODE);
?>
But now i need to filter those files (probably before this code is even executed). acording to the string in their name.
files are named in the following manner:
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.123456789.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.9.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.458.jpg
i need to filter out only ones that have certain number within that string of numbers at the end (between "." and ".jpg") eg. number 9
$number = 9
i was trying with this piece of code to seperate only that last part of name:
<?php
function getBetween($jpgname,$start,$end){
$r = explode($start, $jpgname);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$jpgname = "yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.12789.jpg";
$start = ".";
$end = ".jpg";
$output = getBetween($jpgname,$start,$end);
echo $output;
?>
and i guess i would need STRIPOS within all of this... but im lost now... :(
You can probably use preg_grep.
It's regex for arrays.
This is untested but I think it should work.
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$find = 9;
$filtered = preg_grep("/.*?\.\d*" . $find . "\d*\./", $images);
The regex will look for anything to a dot then any number or no number, the $find then any or no number again and a dot again.
Is this what you need ? It will give you 123456789
$string = "yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.123456789.jpg";
$explode = explode(".", $string);
echo ($explode[1]);
Edit -
As per your requirement Andreas's solution seems to be working.
This is what I tried , I changed the find variable and checked.
$images = array("yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.12789.jpg");
$find = 32;
$filtered = preg_grep("/.*?." . $find . "./", $images);
print_r($filtered);
I've managed to cut the string a sentence into a word. but the new results can be viewed in the browser when the program runs. but these results can not change the condition of the strings in the original text file. I want the contents of the original text file identical to compile the results in the browser. Well how ya how to store the results of the pieces of the word to the text file? in this case stored in notepad with a .txt extension.
To cut the text I use the following php code:
$width = strlen($openfile)/28000;
$wrapped = wordwrap($openfile, $width,'<br>');
//echo $wrapped;
$stringedit=str_replace(" ", "<br>", $openfile);
echo $stringedit;
result from browser is like this
You can use:
file_put_contents ( $fileName, $stringedit); //here filename indicates the name/path of source file.
the solution for it problem is like this ,it is 100% work:
<?php
$array_filename = glob('simpantoken/*.txt');
foreach ($array_filename as $fileteks)
{
$stringteks = file_get_contents($fileteks);
$konversi = strtolower($stringteks);
$jenistandabaca = array(',', '!', '?', '.', ':',';', '-');
$hapustandabaca = str_replace($jenistandabaca,'',$konversi);
$hapustandabaca = trim(preg_replace('/[^0-9a-z]+/i','', $konversi));
$hapustandabaca = preg_replace('/[^a-z\d]+/i', '', $konversi);
$hapustandabaca = preg_replace('/[^\w]+/','',$konversi);
$hapustandabaca = preg_replace('/\W+/','',$konversi);
$replacespasi = str_replace(" ", PHP_EOL, $konversi);
$konversistring = explode("/", $konversi);
$array = preg_split('/[\pZ\pC]+/u', $konversi);
$ubahkarakter = str_replace(" ", '<br/>', $konversi);
if(strpos($konversi,' ') > 0)
{
echo "ada spasi";
}
else
{
echo "tidak ada spasi";
}
$handle = fopen($fileteks, 'w');
fwrite($handle, $replacespasi);
fclose($handle);
}
?>
I have the following code :
function removeFilename($url)
{
$file_info = pathinfo($url);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.php";
$url2 = "http://website.com/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); //outputs http://website.com/folder/
echo removeFilename($url2);//outputs http://website.com/folder/
echo removeFilename($url3);//outputs http:///
Now my problem is that when there is only only a domain without folders or filenames my function removes website.com too.
My idea is there is any way on php to tell my function to do the work only after third slash or any other solutions you think useful.
UPDATED : ( working and tested )
<?php
function removeFilename($url)
{
$parse_file = parse_url($url);
$file_info = pathinfo($parse_file['path']);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.com";
$url2 = "http://website.org/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); echo '<br/>';
echo removeFilename($url2); echo '<br/>';
echo removeFilename($url3);
?>
Output:
http://website.com/folder/
http://website.org/folder/
http://website.com/
Sounds like you are wanting to replace a substring and not the whole thing. This function might help you:
http://php.net/manual/en/function.substr-replace.php
Since filename is at last slash you can use substr and str_replace to remove file name from path.
$PATH = "http://website.com/folder/filename.php";
$file = substr( strrchr( $PATH, "/" ), 1) ;
echo $dir = str_replace( $file, '', $PATH ) ;
OUTPUT
http://website.com/folder/
pathinfo cant recognize only domain and file name. But if without filename url is ended by slash
$a = array(
"http://website.com/folder/filename.php",
"http://website.com/folder/",
"http://website.com",
);
foreach ($a as $item) {
$item = explode('/', $item);
if (count($item) > 3)
$item[count($item)-1] ='';;
echo implode('/', $item) . "\n";
}
result
http://website.com/folder/
http://website.com/folder/
http://website.com
Close to the answer of splash58
function getPath($url) {
$item = explode('/', $url);
if (count($item) > 3) {
if (strpos($item[count($item) - 1], ".") === false) {
return $url;
}
$item[count($item)-1] ='';
return implode('/', $item);
}
return $url;
}
is there any php functions, to sanitize link+path?
i.e.
http://example.com/fold1/fold2/fold3/../../././MyFile.HTML
to
http://example.com/fold1/MyFile.HTML
so, i want remove dots,but maintain the suitable(relative) correct path.
I've found so far, is :
echo ConvertDotedPathToNormalUrl('http://example.com/directory/.././pageee.html');
code:
function ConvertDotedPathToNormalUrl($url){
$firstType = '/(.*)\/((?:(?!\.\.).)+)\/\.\.\//si';
preg_match($firstType,$url,$result);
if (!empty($result[2])){
$url = str_replace('/'.$result[2].'/..','',$url);
if ( strstr($url,'../')){$url= ConvertDotedPathToNormalUrl($url);}
}
$url = str_replace('/./','/',$url); $url = str_replace('://','|||',$url);$url = str_replace('//','/',$url);$url = str_replace('|||','://',$url);
return $url;
}
p.s. but not, it converts
You can
1) get the $path using parse_url(..).
2) get the $webroot = $_SERVER['DOCUMENT_ROOT'];
3) get the $zrealpath = realpath($webroot . $path);
<?php
define ('CRLF', "<br />\n");
$url = 'http://example.com/fold1/fold2/fold3/../../././MyFile.HTML';
$parsed = parse_url($url);
echo '---- vardump($parsed):', CRLF; // for education
zvardump($parsed);
$webroot = $_SERVER['DOCUMENT_ROOT'];
echo 'webroot = ', $webroot, CRLF;
$path = $parsed['path'];
echo 'path = ', $path, CRLF;
$zrealpath = realpath($webroot . $path);
echo 'realpath = ', $zrealpath, CRLF;
function zvardump($var1) {
ob_start();
echo "<pre style=\"margin:0;\">\n";
var_dump($var1);
echo "</pre>\n";
$zoutput = ob_get_contents();
ob_end_clean();
echo str_replace("=>\n ", " => ", $zoutput);
}
?>
i have a text (text.txt) file like this:
shir
beer
geer
deer
i have also a php page with that source:
<?php
foreach (glob("*.txt") as $filename) {
$file = $filename;
$contents = file($file);
$reverse = array_reverse($file, true);
$string = implode("<br>" , $contents);
echo $string;
echo "<br></br>";
}
?>
I want that in the php page it will show:
deer
geer
beer
shir
from the end of the file to the beginning.
thank you
Looks like you are reversing the file name and not the contents.
Do
$reverse = array_reverse($content); // you can drop 2nd arg.
$string = implode("<br>" , $reverse);
in place of
$reverse = array_reverse($file, true);
$string = implode("<br>" , $contents);
Also you can remove the temp variables from you script and do:
foreach (glob("*.txt") as $filename) {
echo implode("<br>" , array_reverse(file($filename))) . "<br></br>";
}
<?php
foreach (glob("*.txt") as $filename) {
$file = $filename;
$contents = file($file);
$reverse = array_reverse($contents, true);
$string = implode("<br>" , $reverse);
echo $string;
echo "<br></br>";
}
?>
Your result was a $contents, without reverse.