how to save the name of a file in mysql using plupload - php

I wonder how I do to save the name of an image in mysql with plupload?
In file upload.php have many variables and I do not know which one stores the filename, already tried everything and could not
I would like to save the image name that plupload creates, not the original name
can someone help me?
thank you

Get the file name like this.
$filename = $_FILES["file"]["name"];
and pass $filename into insert statement.

Try this plugin for plupload to upload file from github

You can directly used a $fileName variable
here give you a example, like
$sSQL = "INSERT INTO eletz_multiimage (image_name) VALUES ('$fileName')";
$result = mysql_query($sSQL);

Related

Unable to add path for image in phpMyAdmin

I was just working on my PHP website and I need database for that, so I was trying to insert image path to phpMyAdmin.
But I am unable to add path because I don't know the exact syntax for image path and also when I am trying to insert image path.
I can't see empty column instead of that. I am watching something like binary and choose file, as you can see in image.
Its good to save files / images in folders and just save their name in database.
<input type="file" name="photo">
$tmpFile = $_FILES['file']['tmp_name'];
$newFile = $_FILES['file']['name'];
move_uploaded_file($tmpFile, '/images/'.$newFile);
$query = "update users set photo = '$newFile' where id = '$userId'";
Here I assume that we are going to upload profile photo of users and saving file in images folder. Also, updating file name in database record.

Uploading image into a db and folder

I have this problem i've created a code for uploading text and image into a database and a folder. Few days back I was wondering how to create a unique id and to be inserted. I've figured it out a way but then the code doesn't upload the image into the database only into the folder.
<?php
if (isset($_FILES['files'])) {
$errors = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file_name = $key . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_size > 2097152) {
$errors[] = 'File size must be less than 2 MB';
}
$query = "
INSERT INTO image_area2
('id',`name`,`path`,`unique_id`)
VALUES
('','$file_name','uploads/$file_name','$_POST[un_id]');
";
The code seems to be fine for the image to be insert into the database but it doesn't work and I can't figure it out. Thank you for the help in advance.
My answears so far so you could see it better:
Well the problem is i don't have any error which is the strangest thing. It works perfectly it inserts the text into the db and the image into the folder but it doesn't insert the path and name into the db. And the code for the insertion is ok i think and i can't figure it why it doesn't do it. I've used print_r($query) function to see if the code is receiving the information from the form and it is receiving it.
Well i've added the mysql_query($query); row and nothing again it has added the text into the db and the image into the folder but nothig about the image into the db. $_FILES['files']['tmp_name'] i use it to define and get the name,tmp name size and type and i've put them into the array $file_name,$file_size,$file_type and $file_tmp
Well i've cleared the id it posted one time but the strange thing is that i've added 1 image and in the db there where 2 lines for the same image and then if i want to add new text and new image again it doesn't work 1 or more images are added into the folder only.
RESOLVED:
The problem was resolved with 2 steps the one that i was written 'id' like that and it wasn't needed anyway and the second the WAMP server was having problems and it was removed and changed. Thank you for all your help :)
If your code is copy/pasted from the actual file, your SQL is using the incorect quote marks in the field list. It should be:
$query = "
INSERT INTO image_area2
(`id`,`name`,`path`,`unique_id`)
VALUES
('','$file_name','uploads/$file_name','$_POST[un_id]');
";
(Notice the back-ticks around id rather than the normal single quote-marks
If id is an auto-incremented primary key then change the SQL to:
$query = "
INSERT INTO image_area2
(`name`,`path`,`unique_id`)
VALUES
('$file_name','uploads/$file_name','$_POST[un_id]');
";
Also, a few pointers:
Sanitize your variables. $_POST['un_id'] is wide open to SQL
injection
Move away from the mysql_ functions - they're
deprecated. Move to mysqli_ or PDO equivalents. This will also
make prepared functions available and remove the need to sanitize
your data (if used properly)

I want to create a file that been given name by the user and save some image in it ? in php

i want to create a file.The name of the file is given by the user.So i take the textfield value to $name and how can i save a file with that name? i know how to save with a static name like
$myfile="ads.txt"
so my question is.is there a way that i can create a file with the name $name and save some stuff in it like image.
$file = fopen($name, 'wb'); // for the love of God and all that is holy, please validate $name
fwrite($file, $data);
fclose($file);
You can't save images in a txt file.
What you must do is save the actual image to your server (uploaded via a file field) and save to a database the location and name of the image, along with the user id, so you know who uploaded it.
There is quite a large amount of code involved, so i would suggest googling for "php file upload tutorial", plenty of examples out there.

is it possible to change the name of a file after it's been uploaded

Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
Put the desired filename in the second argument of move_uploaded_file().
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
$userid = 5; // say you fetch it from database
$ext = explode("\/",$_FILES["file"]["type"]); //extract the file extension
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid.$ext[1]);
UPDATE:
I think you don't need to extract file extension.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid);

how to run sql query after image upload in php when i am using flash uploader for multiple file uploading

I am using library to upload multiple images in php .This library use flash file and a php file . All things are working but problem is that I want to run a query after each image is uploaded to store image detail in database . If i write query in php file after uploading code then It upload images but no error given and no image details are put in database.
Can any one help me that how I add image details in database after uploading image using flash uploader
This is my code
<?
extract($_GET);
$filename = $_FILES['Filedata']['name'];
$temp_name = $_FILES['Filedata']['tmp_name'];
$error = $_FILES['Filedata']['error'];
$size = $_FILES['Filedata']['size'];
/* NOTE: Some server setups might need you to use an absolute path to your "dropbox" folder
(as opposed to the relative one I've used below). Check your server configuration to get
the absolute path to your web directory*/
if(!$error){
copy($temp_name, '../dropbox/'.$filename);
$news_query="insert into tbl_news(img_id,headline,caption,news_catgory_id,shooting_date) Values('0000','News_Headline','News_Caption',
'New_Category','now()')";
mysql_query($news_query) or die(mysql_error());
}
?>
all code working but query data is not updated in database
May be it's a sql error...if your "shooting_date" field is numeric or date, you can't set it's value like this: 'now()', you must strip the quotes.

Categories