I'm having a php error can someone provide some solutions - php

I am having an error in my PHP code and I need help. If anyone knows the solution to this issue, please help me. The error message I am seeing is
[Fatal error: Uncaught BadMethodCallException: Method createtemplate is not defined. in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php:148 Stack trace: #0 C:\xampp\htdocs\php\index1.php(10): PhpOffice\PhpWord\PhpWord->__call('createtemplate', Array) #1 {main} thrown in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php on line 148].
This is my code
<!-- HTML form for selecting the Word document to convert -->
<form method="post" enctype="multipart/form-data">
<label for="word-file">Select Word document:</label>
<input type="file" name="word-file" id="word-file">
<input type="submit" value="Convert">
</form>
<?php
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the uploaded Word document
$wordFile = $_FILES['word-file']['tmp_name'];
// Include the PHPWord library
require_once 'vendor/autoload.php';
// Create a new PHPWord object
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
// Load the Word document
$document = $PHPWord->loadTemplate($wordFile);
// Save the document as a PDF
$document->saveAs('converted.pdf');
// Show a message to the user
echo '<p>The Word document has been converted to PDF.</p>';
}
?>
have tried many solutions but nothing works....

I think you can use save method not to loadTemplate...
please replace your code...
From:
$document = $PHPWord->loadTemplate($wordFile);
$document->saveAs('converted.pdf');
To:
$document = $PHPWord->save($wordFile);

Through the log error, I think you didn't clarify method createtemplate in your PhpOffice\PhpWord\PhpWord.php file!
So it should be worked if you declare that method!

Related

Image converting in PHP

I have a book where is a sample for creating a whole new picture, from a random picture given by the user, and I think there is something wrong in the example, at least I can't use it. The error code gave back by the PHP in the browser:
Fatal error: Uncaught Error: Call to undefined function imagecreatefromstring() in C:\xampp\htdocs\kepkonvertalas.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\kepkonvertalas.php on line 13
The HTML form:
<form action="kepkonvertalas.php" method="post" enctype="multipart/form-data">
<input type="file" name="kep"><br>
<input type="submit" value="kuldes">
</form>
The PHP part:
<?php
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);
$forraskep = imagecreatefromstring($kep);
$szelesseg = imageSX($forraskep);
$magassag = imageSY($forraskep);
$ujmagassag = 400;
$ujszelesseg = $ujmagassag*($szelesseg/$magassag);
$ujkep = imagecreatetruecolor($ujszelesseg, $ujmagassag);
$eredmeny = imagecopyresampled($ujkep, $forraskep, 0, 0, 0, 0, $ujszelesseg, $ujmagassag, $szelesseg, $magassag);
imagejpeg($ujkep, "ujkep.jpg");
?>
<img src="ujkep.jpg" alt="">`
What is suspect, is that this extension is not implemented into my php.ini, but I cant find any proof for it, either solve the problem. Thanks for the help!
I tried to implement a picture from an URL, and do the conversion on that, which means I deleted the
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);`
And I've made a variable with a picture URL, and in the
$forraskep = imagecreatefromstring($kep);
I just changed like this:
$forraskep = imagecreatefromstring(file_get_contents($src);
Well the next problem was, that the $forraskep variable was not defined.
I expect an output where I have the picture with the size of the given parameters.
The solution was actually kinda easy:
open php.ini
search for "extension=gd"
delete the semicolon
restart apache server

Cloudinary upload php failed, how to fix it?

I try using cloudinary for upload my image and video for my personal blog but it's a fail. I watch this video tutorial https://www.youtube.com/watch?v=oZCQLjfq97o. the error says :
Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 8
Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 9
Fatal error: Uncaught Cloudinary\Error: Missing required parameter - file in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php:558 Stack trace: #0 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(407): Cloudinary\Uploader::call_api('upload', Array, Array, NULL) #1 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(100): Cloudinary\Uploader::call_cacheable_api('upload', Array, Array, NULL) #2 C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php(11): Cloudinary\Uploader::upload(NULL, Array) #3 {main} thrown in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php on line 558
This is my code for upload :
require 'vendor/autoload.php';
require 'config.php';
if (isset($_POST['Simpan'])) {
$nama = $_POST['nama'];
$slug = $_POST['slug'];
$gambar = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name'];
\Cloudinary\Uploader::upload($file_tmp, array('public_id' => $slug));
}
and this is the code for form :
<form method="POST">
<input type="text" name="nama">
<input type="text" name="slug">
<?php echo cl_image_upload_tag('image_id');?>
<input type="submit" name="Simpan" value="Simpan">
</form>
The cl_image_upload_tag is used to perform direct uploading from the browser to Cloudinary. When you submit the form you there is no file parameter sent to your backend and thus the Cloudinary::Uploader::upload call fails with the error Missing required parameter - file.
If you would like to upload the file from your backend then you need to replace the cl_image_upload_tag with a regular file input field element. Once a file is selected and the form submitted your backend can use the same code to upload the file to Cloudinary.
On the other hand, if would like to use cl_image_upload_tag then please see the direct uploading section of the Cloudinary documentation that describes setting this up.
https://cloudinary.com/documentation/php_image_and_video_upload#direct_upload_file_tag

How to display divs after checking password with an HTML form

I'm programming a simple password checker with a html form, where you input the code and this code is compared as hash to another hashed code in the database. If the password matches, the index.php file shows a few divs, which otherwise are completely hidden from the user.
I've tried things like:
$input = "";
<center>
<form action="" method="GET">
CODE:<br>
<input type="text" name="code" value="">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
<?php
$checker = new Checker();
$input = $_GET['code'];
//echo "<style>.Main_Content{ visibility:hidden;}</style>";
if ($checker->compareWords($input, 'Beginners')) {
echo "success Beginners<br>";
include 'Beginners_Video.php';
echo "<style>.Main_Content{ visibility:show;}</style>";
}
else {
echo "false Beginners<br>";
}
?>
Fatal error: Uncaught TypeError: Argument 1 passed to
Checker::compareWords() must be of the type string, null given, called
in D:\www\www91\members\members.php on line 64 and defined in
D:\www\www91\members\checker.php:23 Stack trace: #0
D:\www\www91\members\members.php(64): Checker->compareWords(NULL,
'Beginners') #1 {main} thrown in D:\www\www91\members\checker.php on
line 23
The result should be no error and just the whole thing hidden, or the output "False Beginners. Nothing else"
I know that i'm just stupid and that the answer is probably really simple. I've tried and I can't figure it out.
Thanks for your help.
Your code should work once you filled in and submitted the form, but there is no check handling the case where $_GET['code'] is unset or empty. Change your if statement to
if (!empty($input) && $checker->compareWords($input, 'Beginners')) {
in order to check for this first. If !empty($input) is false, the next condition won't be checked at all, so there won't be an error message and it will jump to the else clause directly.
On a side note: You should think about whether GET is the right method for what you're trying to achieve or whether POST would be better. With GET, The codes users enter might get saved in the browser's history and be visible to others using the same computer. Also, for passwords use type="password" for the input instead of displaying it in cleartext.
And one more thing: the <center> tag is deprecated and you should use CSS instead for layout.
At the very beginning you should check if something is passed in code parameter, using the isset() method. If $_GET['code'] is empty it will return null and this is why you get must be of the type string, null given.
<?php
if(isset($_GET['code']) {
$input = $_GET['code'];
} else {
// 'code' parameter is empty
$input = '';
}
$checker = new Checker();
// ...

numerous errors handling file upload in php

I'm using this code to process uploaded files:
mkdir("files/" . $id, 0700);
$path = "files/" . $id;
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
foreach($_FILES['attachments']['name'] as $f => $name)
{
if($_FILES['attachments']['error'][$f] == 4)
{
continue;
}
if($_FILES['attachments']['error'][$f] == 0)
{
if(move_uploaded_file($_FILES["attachments"]["tmp_name"][$f], $path.$name))
{
$count++;
}
}
}
}
$id is a random number taken from the database. Besides, I'm using this markup:
<input type="file" id="attachments" name="attachments[]" multiple="multiple" accept="*">
While the exact same code had worked brilliantly before, it now throws numbers of errors I can't really deduce:
1: mkdir(): File exists in ... on line ... (<-- now, it doesn't for granted!)
2: Undefined index: attachments in ... on line ... (well, it's defined also using form method post!)
3: Invalid argument supplied for foreach() in ... on line ... (which is quite clear as the above stated errors do prevent the foreach from doing its job correctly)
Yes, I made sure that I'm actually using POST. I also tried changing the file input's name from attachments to any other, however, scenario remains the same.
Adding enctype="multipart/form-data" has done it.
1] Check for the folder rights 0777. Weather you are able to create directory or not
2] After posting form. Make sure your form has enctype = multipart/form-data tag.
In you file please check with
echo "<pre>";
print_r($_FILES);
exit;
If you are getting any data or not? If getting data then move ahead.
First check if your $id really contain something, secondly your form should have attribute of enctype = multipart/form-data for using input type file.
<form action="test.php" method="post" enctype="multipart/form-data">
Now in your case you will get the array of files you before your perform any work, see print_r of attachments:
echo "<pre>";
print_r($_FILES);
exit;

Using a form to append Nodes to XML using PHP

I'm really new to PHP and Im trying to append Nodes to XML using PHP.
This is my comments.php file:
<?php
$date = "http://jdrag.x10.mx/comments_file.xml";
$xml = simplexml_load_string($data);
$commentt = $xml->addChild("comment");
$name = $_POST["cname"];
$email = $_POST["cemail"];
$comment = $_POST["comment"];
$commentt->addChild("name", $name);
$commentt->addChild("email", $email);
$commentt->addChild("commentInside", $comment);
echo $xml->saveXML();
?>
My comments.html is:
<form action="comments.php" method="post">
Name: <input type="text" name="cname" />
Email: <input type="text" name="cemail" />
Comment: <input type="text" name="comment" />
<input type="submit" />
</form>
As you can see my XML file is simple the comments tag: http://jdrag.x10.mx/comments_file.xml
But when I submit the form I get this error:
Fatal error: Call to a member function addChild() on a non-object in /home/jdragx10/public_html/comments.php on line 5
And because i'm new to PHP I really don't know what it means or how to fix it.
Thanks in advance to anyone who can fix my code.
It looks like you want to be using simplexml_load_file().
The simplexml_load_string() function takes the XML document as a string whereas simplexml_load_file() takes the path to a file containing the XML, as you have.
You also have some problem with variable names, the path to the XML file is held in $date whereas you try to load the XML using the variable $data.
Finally, be sure to turn on (and to the maximum level) error reporting while you are writing your code. The code above gives a notice message ("Notice: Undefined variable: data…"), not just a fatal error message.
Set the values display_errors and error_reporting to On and -1 respectively, either in your php.ini file or using ini_set().

Categories