I'm trying to write a php for uploading pictures onto server (which works) and updating url link in the database. (which doesn't)
The problem bit is like this:
< ?php
require_once('Db.php');
Db::connect('localhost', '***', '***', '***');
// ... some stuff for uploading files ...
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "File ". basename( $_FILES["fileToUpload"]["name"]). " was uploaded.";
$_POST['path_url']= ".../images/". basename( $_FILES["fileToUpload"]["name"]);*
$query=Db::connect(
'UPDATE tab_digital SET `path_url`=(?) WHERE `id_digital`=(?)',
$_POST['path_url'], $_POST['id_digital']
);
}
? >
I tried echoing $_POST[''] both path_url and ID after that and it worked. (ID is from a simple input form)
Related
<?php
include 'db.php';
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$stnam = $_POST['stName'];
$stage = $_POST['stAge'];
$stdob = $_POST['stDob'];
$pic=($_FILES['photo']['name']);
mysqli_query("INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', 'pic')");
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
I have been trying to upload an image file into sql database using phpmyadmin I used this above code when I executed it the file goes into directory well but the data values are not being inserted into mysql. Can anyone help?
Changes
1) Put $ before pic in insert query.
2) And, keep insert query inside if(). Because, row will get inserted even though file is not moved to desired folder.
3) No connection variable is used in mysqli_query. It requires 2 arguments. [I assumed $conn as connection variable. Replace it according to your connection variable.] For more info, click mysqli_query
Updated Code
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
mysqli_query($conn, "INSERT INTO test (name, age, dob, photo) VALUES ('$stnam', '$stage', '$stdob', '$pic')");
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
} else {
echo "Sorry, there was a problem uploading your file.";
}
I am making an application on my server, where the user uploads an image through some HTML combined with javascript.
The user finds an image on the computer through
<form action="uploadimage.php" method="post"
enctype="multipart/form-data">
<label for="file">Filnavn:</label>
<input type="file" name="file" id="file" value="100000" />
Then the point behind the javascript, is to validate on the users image
if(picture_headline.value == "" || picture_uploaded.value == "" || !ischecked)
{
// Don't execute, stay on same site
}
else
{
// execute php and upload image
}
the php is an upload image php script
<?php
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/";
// add the original filename of our target path
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
// Moves the uploaded file into correct folder
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
I must say I am a bit confused here, since I have only been working with html, php and javascript for a few days now.
Am I totally off or what?
I found some "simple" examples online, which I put on my server through cuteFTP, but everytime i press upload, the website just sends me to the .php file and says the site doesn't exist.
Like Boann points out you're trying to access a non-existent file in your PHP code ("uploaded" and "uploadedfile" rather than "file" (which is what you named the field in your HTML form)).
But regarding "running PHP from JavaScript": You don't have to. The JavaScript should only return false if the form is invalid. If it's valid you don't need to do anything and the form will submit, in turn running your PHP script:
form.onsubmit = function () {
if (!formIsValid()) {
return false;
}
};
If the form is invalid it won't submit (the return false bit (you could use event.preventDefault() instead)), if it is valid nothing will happen and the form will do what it does (ie submit the data to the server).
Each array key in $_FILES corresponds with the name attribute of a file field in the form, so to match your form it should be 'file' rather than 'uploaded' or 'uploadedfile':
<?php
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/";
// add the original filename of our target path
$target = $target . basename( $_FILES['file']['name'] ) ;
$ok=1;
// Moves the uploaded file into correct folder
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
I'm doing a simple file upload using the following script:
$errors = '';
$target_path = "[PATH HERE]";
$target_path = $target_path . basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
For some reason, I get an error uploading the file and the file type is not displayed. It seems to only grab the name of the file without the extension (i.e. "test" rather than "test.pdf"). I'm sure it's something simple, but what am I doing wrong?
If you check the error element in the files array, you'll probably find it's some value other than 0. The error should be 0 if nothing went wrong. Otherwise, compare the value stored in error against the PHP documentation to determine what went wrong.
Perhaps your entering the path wrong (ending slash), or php dont have permission to write to the directory.
<?php
error_reporting(E_ALL); // Show some errors
$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash
$target_path = $target_path.basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
?>
Hi I am quite new to php but i have been following some tutorials but they don't seem to work so I have tried to adapt them.
I have tested this code and it works to a point but theres something else I can't get my head around, the php file is not uploading (fine) but the details are still being writen to the datbase although the $ok is spose to be set to 0 (not fine). It might be easier if explain what is ment to happen here:
-The User can upload gif or jpeg files. Details added to the db.
-The User can upload no file as a default will be used. Details added to the db.
-The User should not be able to upload any other file. No record should be on the db, user should have to try again.
My Code so far:
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$ok=0;
//This gets all the other information from the form
$name= mysql_real_escape_string ($_POST['nameMember']);
$bandMember= mysql_real_escape_string ($_POST['bandMember']);
$pic= mysql_real_escape_string ($_FILES['photo']['name']);
$about= mysql_real_escape_string ($_POST['aboutMember']);
$bands= mysql_real_escape_string ($_POST['otherBands']);
$uploaded_size=$_FILES['photo']['file_size'];
if ($uploaded_size > 350000)
{
echo "Your file is too large, 35Kb is the largest file you can upload.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if (!($uploaded_type =="image/jpeg"))
{
echo "JPEG<br>";$ok=1;
}
if ($uploaded_type =="image/gif")
{
echo "GIf<br>";$ok=1;
}
if (empty($pic)){
echo "You haven't uploaded a photo, a default will be used instead.<br/>";$ok=1;}
if ($ok==0)
{
Echo "Sorry your file was not uploaded, please try again with the correct format.";
}
//If everything is ok we try to upload it
else
{
// Connects to your Database
mysql_connect("localhost", "*******", "******") or die(mysql_error()) ;
mysql_select_db("project") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO dbProfile (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory<br/>";
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>";
}
else {
//Gives and error if its not
echo "<p>If you have uploaded a picture there may have been a problem uploading your file.</p>";
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>";
}
}
?>
Cheers in advance. CHL
The error probably is this if statement:
if (!($uploaded_type =="image/jpeg"))
{
echo "JPEG<br>";$ok=1;
}
Because every time you upload an image that does not have a content type that equals "image/jpeg", $ok evaluates to 1, so everything gets written to the database.
But also notice, that just checking the MIME type like this can get you into trouble, since the user is able to fake the MIME type of a file.
You could use Imagick to get the correct image MIME type, for example. See more details here: http://de2.php.net/manual/en/function.imagick-identifyimage.php
Edit: Just noticed, that $uploaded_type does not get initialized anywhere in your script. As I said, you can do a rough estimation of the MIME type by using $_FILES['photo']['type'].
$result = mysql_query("SELECT * FROM media WHERE path = '$target'");
if($row = mysql_num_rows($result)==1)
{
echo"<br />Sorry, there is already a file with that name on the server.<br />Please press back on your browser and save the file under a different name.";
}else{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
mysql_query("INSERT INTO media (id, related_page_id, type, title, copy, path, position, assets, time) VALUES ('', '$cat','$type','$name','','$target','$position','$id','$today')");
header("Location: edit.php?category=$cat");
exit();
echo $name;
}
else {
echo $today;
echo "<br />";
echo "Sorry, there was a problem uploading your file. Please press back on your browser and try again.";
}
}
The above code used to work. Now it will not let me upload files to ../uploads/ but it works fine if i upload to uploads/.
Does anyone have any suggestions as to what i'm doing wrong? Thanks
EDIT
$target = "../uploads/";
I know it sounds silly but are the permissions for the folder set up correctly?