I am new to PHP. here is my code to save results of form to database and uploaded file name.I am using uploadify to upload files.
Here i have 2 problems.
file saves on its location in mozila but do not works for chrome.
coded goes here.
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$picAd= $_FILES ['Filedata']['name'];
// to change same file name
$picAd=rand(0,1000)."_".rand(0,1000)."_".$picAd;
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
// this line works ok for all browsers
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
this line not works for chrome
// $targetFile = str_replace('//','/',$targetPath) . $picAd; <<Error for chrome
if( move_uploaded_file($tempFile,$targetFile)){ // 4
echo true;
}else{
echo false;
}
saving results to database .
static $picAd;
if(isset($_POST['submit'])){
// varibles
$uname=$_REQUEST['userName'];
$pw=$_REQUEST["pw"];
$email= $_REQUEST["email"];
$twit= $_REQUEST["twit"];
$user="user";
if($picAd != ""){
$q="insert into users values(Null,'$user','$uname','$pw','$email','$twit','$picAd')";
mysql_query($q);
mysql_close($dbc); }
}
Here i am not getting the value of $picAd in database its value goes NUll.
Related
I am trying to upload images from registration form in Yii framework. The image will be saved in "img/avatar" folder and the name of the image should be changed to the username. The piece of code I use for this is below:
//uploading avatar to the img/avatar folder
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$personModel->picture = $upload_file;
$picture_name = $userModel->username;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name);
}
$personModel->save();
//end of image uploading part
The problem is: the name of the username has been saved in picture row of the database. But the image was not uploaded to the folder. I am trying to find out the problem in the code. but cannot solve it. Any suggestions?
Well first thing you need to do is to prevent database input if the picture is not saved.
if(isset($uploadedfile))
{
if($upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name)
{
$personModel->save();
}
else
{
//throw error
}
}
As far as problems in the code go. Most common problem is directories not existing, path to them not being correct.
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$ext = pathinfo($upload_file->picture, PATHINFO_EXTENSION);
$picture_name = $userModel->username . '.' . $ext;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs('/Your_correct_path/.../etc/'.$picture_name);
}
$personModel->save();
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$picture_name = $userModel->username . '.' . pathinfo($upload_file, PATHINFO_EXTENSION);
$personModel->picture = $picture_name;
if (isset($upload_file)) {
$upload_file->saveAs(Yii::app()->basePath . '/../img/avatar/' . $picture_name);
}
$personModel->save();
You have to check your folder permission.
The problem has been solved through following code:
$uploadFile = CUploadedFile::getInstance($personModel, 'picture');
$extension = pathinfo($uploadFile, PATHINFO_EXTENSION);
$fileName = $userModel->username . '.' . $extension;
if (isset($uploadFile)) {
$personModel->picture = $fileName;
$uploadFile->saveAs(Yii::app()->basePath . '/../img/avatar/' . $fileName);
}
This is a file that receives uploaded images from another page. The images are received and saved on the server (that part is working) the query is not. I manually typed the query into phpMyAdmin with dummy values for the file name and it works. I put that dummy query into this code and it doesn't work. I can't figure out what I'm doing wrong.
<?php
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'img';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']. $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
mysql_query("INSERT INTO `photos` (photo_id, file_name) VALUES ('', '".$_FILES['file']['name']."');");
}
?>
Assuming that photo_id is auto incremented here are some fixes to your code:
<?php
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'img';
if (!empty($_FILES)) {
$name = $_FILES['file']['name'];
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']. $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
$query = "INSERT INTO photos (photo_id, file_name) VALUES (NULL, '".$name."')";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
}
?>
This way you should be able to debug your code.
Now if you need to get the result from a Javascript call, you should use AJAX.
Check here:
Send a request to a php page and then get back results using ajax
I'm trying to upload an image to the server using a PHP script. It works fine, till I want to change the filename using a variable in the name.
I want to rename the file to the logged in users username_filename.extension, but it just skips the username variable so the name becomes: _filename.extension.
<?php
session_start();
include "./global.php";
$res = mysql_query("SELECT * FROM users WHERE id='".$_SESSION['uid']."'");
$row = mysql_fetch_assoc($res);
$username = $row['username'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$ext = explode('.',$_FILES['Filedata']['name']);
$extension = $ext[1];
$newname = '/var/www/picturebox/albums/' . $username . '_' . $ext[0] . '.' . $ext[1];
move_uploaded_file($tempFile,$newname);
}
?>
And the filename becomes _filename.extension
Anyone knows what to do?
Just found out that it works fine in IE, but now in Chrome. Any suggestions on a solution?
I am using Uploadify for my script.
The main page:
<?php
session_start();
var_dump($_SESSION);
$uploaded_files = $_SESSION['uploaded_files'];
?>
//Uploadify, HTML forms and more (not related, No PHP in this section)
uploadify.php:
<?php
session_start();
require_once('includes/functions.php');
// Define a destination
$targetFolder = 'uploads/temp'; // Relative to the root
if (!empty($_FILES)) {
$fileParts = pathinfo($_FILES['Filedata']['name']);
$file_hash = GenRndStr(20) . '.' . $fileParts['extension'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $file_hash;
// Validate the file type
$fileTypes = array(); // File extensions
move_uploaded_file($tempFile, $targetFile);
$_SESSION['uploaded_files'][] = $file_hash;
echo '1';
}
?>
I'm sure it gets to the $_SESSION['uploaded_files'][] = $file_hash; part, since the actual file is uploaded to the directory. My problem is that the var_dump of $_SESSION['uploaded_files'] returns null.
The files are in the same directory level.
Thanks in advance.
I found the solution. The problem is that Uploadify's flash version is treated as a different client for the server, therefore the server creates a new session id for it.
I followed this topic: http://www.uploadify.com/forum/#/discussion/43
Hope it'll help others.
This is my code -
<?php
session_start();
include('connect.php');
mysqli_select_db($connect, "users");
$s = "select * from name where sessionusername = '$u'";
$q = mysqli_query($connect, $s);
$f = mysqli_fetch_array($q);
$name = $f['name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);
// Get the extension, and build the file name
//$extension = pathinfo($tempFile, PATHINFO_EXTENSION);
$extension = end(explode(".",$_FILES['Filedata']["name"]));
$new_file_name = '".$name."'".".$extension;
$targetFile = str_replace('//','/',$targetPath) . $new_file_name;
// $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
?>
Why is the above not working? As you can see, I am trying to pull down the name from the users database, and then rename the uploaded file to the name that was pulled from the db.
Can you help me out? Thanks a lot.
Is form enctype == 'multipart/form-data' ?
Ah, now I understand what you're talking about. You should remove your other post. This is an error I've actually encountered with Uploadify before, but I'm not sure what is going on here, specifically. Definitely, checkout your enctype, but for debugging, I implemented this solution for error reporting with Uploadify here: http://www.uploadify.com/forums/discussion/14/upload-script-error-reporting/p1.