So I'm getting a Pdf file send to my webservice for me to upload,
it's send to me as a base64Binary type variable,
and uploaded using below script :
function SaveEIM($parameters)
{
mysql_query("INSERT INTO tablename (Filename, FileSize, File)
VALUES ( '" . $parameters['Filename'] . "', '". $parameters['FileSize'] . "', '" . $parameters['File'] . "' ) ") or die (mysql_error());
}
Now everything is getting uploaded, only the weird thing is, for instance if I upload an 370KB file, it's 490KB in the Database....
I don't know how this happens, and if this is normal?
Now to save it in my fileserver :
$query = mysql_query("SELECT File FROM tablename WHERE ID = '1' ") or die (mysql_error());
$fetch = mysql_fetch_array($query);
$data = $fetch[0];
file_put_contents("user_123.pdf", $data);
When I open this file that is getting saved now in my fileserver, I get this error : "user_123.pdf cannot be opened because this filetype is not supported or because it is damaged."
I'm fairly new to Blob types, and I know that storing files on your server is sometimes better, but I have to use Blob times, since I was asked to do so.
If anyone could help me out here, it would be greatly appreciated.
Solved it, had to use base64_decode($data) on my blobtype.
Related
I knew that there are several similar questions, also I am aware that best practice is to keep images on server but I was told to do this way... Well I am sending base64 string from android to php webservice. I am sending it right, also tested via Postman to be sure that problem is not android but service. I have error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'oX?떧?=j?b?ED?Nϯ??=?|?pv??ёQv}gX?' at line 2
<?php
header('Content-Type: text/html; charset=utf-8');
// array for JSON response
$response = array();
// include db connect class
require_once '../config/db_connect.php';
$db = new DB_CONNECT();
$host_id = $_POST['host_id'];
$name = $_POST['event_name'];
$description = $_POST['event_description'];
$date = $_POST['date'];
$photo = $_POST['photo'];
// get all products from products table
$escaped = mysql_escape_string ($photo);
$photo_blob = base64_decode($escaped);
//echo $photo_blob;
$result = mysql_query(
'INSERT INTO dogadjaj (host_id, name, description, date, photo)
VALUES ("' . $host_id . '" ,"' . $name .'", "' . $description . '", "' . $date . '", "' . $photo_blob . '");')
or die(mysql_error());
?>
Use $photo_blob = base64_encode($escaped); instead of $photo_blob = base64_decode($escaped);. You would need the base64_decode when processing the base_64 read from the database.
I am trying to restore a database to a different server through PHP. I managed to execute all needed commands but the database keeps hanging in the 'Restoring...' state on the server.
I have searched and followed the answer on SQL Server: Database stuck in “Restoring” state with PHP (which leads to this article) but this did not work for me; I get an error when trying to change the environment to the newly recovered database.
Code:
sqlsrv_configure( "WarningsReturnAsErrors", 0 );
$connOptions = ["Database"=>"master"];
$sqlConnection = sqlsrv_connect("server_name\\SERVER", $connOptions);
sqlsrv_query($sqlConnection, "USE master");
$sql = "IF EXISTS(SELECT name FROM sys.databases
WHERE name = 'db_name')
DROP DATABASE db_name";
sqlsrv_query($sqlConnection, $sql);
$sql = "RESTORE FILELISTONLY FROM DISK='$path'";
$logicalNamesStatement = sqlsrv_query($sqlConnection, $sql);
$moveArray = [];
while($logicalNames = sqlsrv_fetch_array($logicalNamesStatement, SQLSRV_FETCH_ASSOC)){
if($logicalNames['Type'] === "D"){
$moveArray['MDF'] = $logicalNames['LogicalName'];
}
elseif($logicalNames['Type'] === "L"){
$moveArray['LDF'] = $logicalNames['LogicalName'];
}
}
$localDbPath = "c:\\Program Files\\Microsoft SQL Server\\MSSQL11.SERVER\\MSSQL\\DATA\\";
$sql = "RESTORE DATABASE db_name FROM DISK='$path'
WITH
MOVE '" . $moveArray['MDF'] . "' TO '" . $localDbPath . "db_name.mdf',
MOVE '" . $moveArray['LDF'] . "' TO '" . $localDbPath . "db_name_log.ldf',
REPLACE,
STATS=10";
sqlsrv_query($sqlConnection, $sql);
$sql = "RESTORE DATABASE db_name FROM DISK='$path' WITH REPLACE, RECOVERY";
sqlsrv_query($sqlConnection, $sql);
sqlsrv_query($sqlConnection, "USE db_name");
The error that comes back is:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Database 'db_name' cannot be opened. It is in the middle of a restore.
Do you have any ideas on how I can solve this? The only authority on a workaround for the bug described in my previously linked article seems to be that exact article, which does not work as you can see.
Thanks!
I don't have 50 reputation yet, so feel free to move this to the comments section. I don't see your database names including brackets "[ ]". If you have any database names with spaces or special characters, you'll need the brackets to identify the object properly. And as Mitch already stated, based on what you are doing, there is no reason for two restore operations; just add the RECOVERY option into the first and be done with it.
For some reason when I upload a document and store the file name in mysql table the file name extension isn't included in the name. For example, I upload the pdf file "car.pdf" and the way it is stored in mysql table is "car."(see period right after the word, The file extension is not included). I have checked this website, I have googled it too, but can't find a clear answer.
$doc_name = mysqli_real_escape_string($dbc, trim($_FILES['doc']['name']));
$doc_type = $_FILES['doc']['type'];
$doc_size = $_FILES['doc']['size'];
if(!empty($title)){
if(!empty($doc_name)){
if (($doc_type == 'application/pdf') || ($doc_type == 'application/msword') || ($doc_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')){
if(($doc_size > 0) && ($doc_size <= FILE_MAXFILESIZE)){
if ($_FILES['doc']['error'] == 0) {
$final_name = time() . $doc_name;
$target = $uploadpath . $final_name;
if(move_uploaded_file($_FILES['doc']['tmp_name'], $target)) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('There was a problem with your query. Please, contact administrator');
if(!empty($description)){
$query = "INSERT INTO uploaded_files (file, title, description, date_time, user_id, dep_id) VALUES ('$final_name', '$title', '$description', now(), '" . $_SESSION['user_id'] . "', '$dep_id')";
}.... rest of the code goes here
MySQL could truncate the string you're inserting, if the length of the string exceeds what you have in the column definition. In this case, check that the file path name does not have a string name longer than 32.
Ordinarily, one would expect this INSERT query to fail, but it may not do so if the configuration settings permit.
If the mysql configuration is under your control (and you desire to avoid this kind of silent truncation of inserted string), start by adding strict_trans_tables to the sql_mode settings in your my.ini (OR my.conf) file.
Best of luck.
So, I wanna to get file path and then insert that into mysql.
This is the code:
<?php
$file = $_FILES['file']['tmp_name'];
include "config.php";
$result = mysql_query("LOAD DATA LOCAL INFILE '$file'" .
" INTO TABLE ponuda FIELDS TERMINATED BY ' '");
if (!$result) {
die("Could not load. " . mysql_error());
}
?>
This is the path I receive:
string 'C:\Users\xxx\AppData\Local\Temp\php11EF.tmp'
But, I got mysql error saying this:
Could not load. Can't find file 'C:UsersxxxAppDataLocalTempphp1FA6.tmp'.
Why are slashes removed? What I doing wrong. Tried to search for a problem but didn't find any results.
p.s. I want to upload that file from my local PC.
EDIT: if I change "\" to this "/" in path it works well, but how can I use original file path, not temp one because that file doesn't exist in temp?
OK, managed to find error.
Now it's working:
<?php
$file = str_replace("\\", "/", $_FILES['file']['tmp_name']);
include "config.php";
$result = mysql_query("LOAD DATA LOCAL INFILE '$file'" .
" INTO TABLE ponuda FIELDS TERMINATED BY ' '");
if (!$result) {
die("Could not load. " . mysql_error());
}
?>
I have a mysql database with a table in the following format:
ID: 1
Date: 2010-12-19
Image: 5d61240f-7aca-d34b-19-12-10-15-36.jpg
Caption: Merry Xmas
I want to create a php script which checks through each row in this table and checks that the image is present in a gallery folder on my server. If the image is not in the gallery folder then I want to delete this row from my database. Some pointers on how to go about doing this would be very much appreciated.
Thanks!
try
<?php
define ("GALLERY_ROOT", "/path/to/gallery/" );
$mysqli = new MySQLi ($host, $username, $password, $db);
$result = $mysqli -> query ("
SELECT
id,
image
FROM
table
");
if ( $result ){
while ($row = $result->fetch_assoc()) {
if ( !is_file (GALLERY_ROOT . $row['image'] ) ){
$mysqli -> query ("
DELETE FROM
table
WHERE
id = '" . $row['id'] . "'
LIMIT 1
");
print "Deleted " . $row['id'] . "<br />";
}
}
$result -> free();
}
$mysqli -> close();
print "Congratz!! All invalid rows has been deleted!";
?>
That was a quick one, i didn't try running it actually.
Also if you have a lot of rows, then you might want to rethink about selecting all the rows at once. But again get back to me on how it works
Ok... use glob() or the DirectoryIterator, file_exists(), the mysql_ or mysqli_ functions, and 'DELETE FROM images WHERE id = ?'. :)
Hope this helps! If you supply some more information, I could give you more detailed advice. What have you tried so far?
EDIT: wait, I misread. You don't need the directory functions, as you already have the filename. dirname(__FILE__) might be useful.