Link with 2 variables in 1 variable - php

I want to find out what the filessize of this file is filesize().
With an echo it works:
$Data[textfile]; = Complete Filename with ending in Database
echo 'http://myurl.com/files/'.$Data[id].'/'.$Data[textFile].'';
But it doesn´t work like this:
$file = "http://myurl.com/files/" . $Data[id] . "/" . $Data[textFile] . "";
The link looks/should look like this:
http://myurl.com/files/123/textfile.docx
edit: sorry for my weird english...i hope the point is clear
Edit 2: I want to get the filesize of a pdf or docx
The rest of the code:
$size = filesize($file);
$size = $size/1048576;
$size = round($size,2);
echo $size." Mb";

try $file = 'http://myurl.com/files/' . $Data['id'] . '/' . $Data['textFile'];

Try:
$file = 'http://myurl.com/files/' . $Data[id] . '/' . $Data[textFile];

try $file = 'http://myurl.com/files/'.$Data[id].'/'.$Data[textFile];

Related

php move-uploaded-file is setting the address as the name of the file

I have two completely similar codes that one of them works and the other doesnt
$id = 1;
$ext = pathinfo($_FILES['adminProfilePicture']["name"], PATHINFO_EXTENSION);
$address = '../img/admin' . $id . 'image.' . $ext;
move_uploaded_file($_FILES['adminProfilePicture']["tmp_name"],$address);
this code works perfectly and saves the file in the directory root/img/admin1image.png
$id = 1;
$ext = pathinfo($_FILES['teamProfilePicture']["name"], PATHINFO_EXTENSION);
$address = '../img/team' . $id . 'image.' . $ext;
move_uploaded_file($_FILES['teamProfilePicture']["tmp_name"],$address);
but this code for some reason sets the address as the name of the file
and saves a file with this name (../img/team1image.png) in the directory root/(../img/team1image.png)
i really dont know what could be wrong
any help would be appreciated
and one other thing is the code works on my localhost
but iam making a project for someone else and this problem occurs on his host
change the address
$address = '../img/team' . $id . 'image.' . $ext;
to
$address = realpath(__DIR__.'/../img/').'team'.$id.'image.'.$ext;

$_FILES returning nothing

So, I recently looked up how to upload files onto a database using Blob. This is the tutorial I found http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
I followed this tutorial and came up with this code -
HTML FORM -
<form action='memberPage.php' method='post' enctype='multipart/form-data'>
<input type='text' name='programName' placeholder='Program Name'></input>
<input type='text' name='programPrice' placeholder='Price'></input>
<div id='uploadFiles'>
<input name='userfile1' type='file' onchange='addAnother()'>
</div>
<input type='submit' value='Create program' style='cursor:pointer;'></input>
</form>
PHP UPLOAD ($programName is specified outside code it looks something like $programName=$_POST["programName"];)
for($i = 1; $i < 999;$i++){
if(isset($_POST["userfile" . $i]) && $_FILES["userfile" . $i]['size'] > 0){
$Filename = $_FILES["userfile" . $i]['name'];
$finfo = new finfo();
$Filetype = $finfo->file($_FILES["userfile" . $i]['tmp_name'], FILEINFO_MIME);
$Filesize = $_FILES["userfile" . $i]['size'];
$Filetmpname = $_FILES["userfile" . $i]['tmp_name'];
$Fileerror = $_FILES["userfile" . $i]['error'];
$fp = fopen($Filetmpname, 'r');
$content = fread($fp, filesize($Filetmpname));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$Filename = addslashes($Filename);
}
mysqli_query($conn, "INSERT INTO upload(name,type,size,content,program) VALUES('
'" . $Filename. "','" . $Filetype. "','" . $Filesize . "','" . $content . "','" . $programName . "')");
}else{
$i=999;
header("location:memberPage.php");
}
}
But when I submit the forum, the files return with nothing in them, no name, and the size is zero.
I have also checked out the other question things on here and did not find the answer, so my guess is that it is in my code.
Let me also specify that the onchange='addAnother()' adds another input, but has the next end number on it (Example - userfile1, it would then add userfile2)
you are using input name "programName" and try to get "Programname" the same with $_POST.
PHP is case sensitive you should use same case in PHP "$_POST['programName']"
We need to check file element istead of post element.
Try changing this
if(isset($_POST["userfile" . $i]) && $_FILES["userfile" . $i]['size'] > 0){
to this
if(isset($_FILES["userfile" . $i]) && $_FILES["userfile" . $i]['size'] > 0){
It would be better to create the input names as arrays.
<input name='userfile[]' type='file' onchange='addAnother()'>
and iterate through it in php like:
if (isset($_FILES["userfile"]) && is_array($_FILES["userfile"])) {
for ($i=0;$i<count($_FILES["userfile"]);$i++) {
echo $_FILES["userfile"]["name"][$i];
}
}

Image is being shown as HTML but no image is shown for DOMPDF()

I try to show image as follows:
function part_profile_record_pdf_form()
{
$sql = "SELECT * FROM part_profile WHERE part_id=" . arg(2);
$query = db_query($sql);
$dataFormDb = db_fetch_object($query);
$url = '../../../' . $dataFormDb->image;
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output = "<style>" . file_get_contents($css_file) . "</style>";
$output .= "<html><body ><div id=\"wrapper\">";
$output.= '<img height="156" width="128" id="img_profile" src="'.$url.'">';
$output.="</div></body></html>";
return $output;
}
I can see the image by the following function:
function part_profile_make_pdf() {
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output= part_profile_record_pdf_form();
print $output;
}
But when I try to make pdf as follows , no image is shown giving 'x' and 'image not readable or empty':
function part_profile_make_pdf() {
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output= part_profile_record_pdf_form();
// print $output;
require_once(realpath(".") . "/sites/all/libraries/dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
if (isset($_SESSION['indv_report_file_name']) && !empty($_SESSION['indv_report_file_name'])) {
$filename = $_SESSION['indv_report_file_name'];
} else {
$rand_val = rand(0, 1000);
$filename = "eureka_" . $rand_val . ".pdf";
$_SESSION['indv_report_file_name'] = $filename;
}
$dompdf = new DOMPDF();
$dompdf->load_html($output);
// $dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'a4');
// "a4" => array(0,0,595.28,841.89),
$dompdf->set_paper(array(0, 0, 595.28, 420), "portrait"); // 12" x 12"
$dompdf->render();
$dompdf->stream("participant-profile.pdf", array("Attachment" => false));
exit(0);
}
I already set DOMPDF_ENABLE_REMOTE to true in my config dompdf_config.inc.php. have any idea. Thanks in advance.
After a little testing in my own DOMPDF environment, I reckon this will be due to the relative path to your image. Currently you're using
$url = '../../../' . $dataFormDb->image;
Which is fine for the preview in HTML, the browser can find the image.
But when DOMPDF tries to process the HTML and find the image, it's doing so from a system perspective, like PHP would if you were doing a file_exists(). So you need to add the full path:
$url = $_SERVER['DOCUMENT_ROOT'] . '/path/to/dir/' . $dataFormDb->image;
or just hardcode it
$url = '/home/username/sitename/public_html/images/' . $dataFormDb->image;
So you might need some logic to say: am I doing a preview? Then use relative path. Or am I doing a PDF render? Then use full path. But I'll leave that to you!

php file put contents reverse

Ok sorry if its a stupid question im a begginer.
Im making a small shoutbox just for practise.
It inserts the shout infos in a txt file.
My problem is that, it lists the text from top to bottom, and i would like to do this reversed.
if(isset($_POST['submit'])) {
$text = $_POST['text'];
if(!empty($text)) {
$text = $_POST['text'];
$name = $_POST['name'];
$time = date("H:i");
$content =
"<div class='text'><em>" . $time . "</em>
<span class='c11'><b>" . "<a href='userinfo_php_willbe_here.php' target='_blank'>" . htmlspecialchars($name) . "</a>" . ":</span></b>
" . htmlspecialchars($text) . "
</div>\n";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
}
here is my code.
i was googleing around with not much luck maybe i wasnt looking hard enough.
could please someone give me a hint?
thank you
No way to do so with one function call. You need to read the content from your target file, prepend the data in php and rewrite the whole file (see file_get_contents).
$fileContent = file_get_contents($file);
$fileContent = $content . $fileContent;
file_put_contents($file, $fileContent, LOCK_EX);
You can also use the array_reverse like so:
// Data in file separated by new-line
$data = explode("\n",file_get_contents("filename.txt"));
foreach(array_reverse($data) as $value) {
echo $value."\n";
}
You can only prepend to a file by means of reading it and writing afterwards.
file_put_contents($file, $content . file_get_contents($file), LOCK_EX);

Simple PHP echo help

How could I add an echo in here
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/ (HERE) .torrent");
I have tried a few things with no luck.
It's just a mere guess, but perhaps you want to do something like that:
$newFileName = 'someFielenameGeneratedByYourScript';
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/" . $newFileName . ".torrent");
You could do something like:
$location = "uploads/" . $name . ".torrent";
move_uploaded_file($_FILES["torrent"]["tmp_name"], $location);
echo $location;
You don't need to echo, since you are passing it to a function not trying to display it in the output.
move_uploaded_file($_FILES["torrent"]["tmp_name"], "uploads/ ".$variable." .torrent");

Categories