Debugging a php script - php

I am using textpad to create php scripts. Now is there anything I can use with textpad, or is there a way to debug with textpad. I am a few of my code echo out and I am still not getting the results I am wanting my page to do. So I am thinking my code needs some debugging. I will post the code below and I am sure many of you will agree it needs debugging too. I know alot of people are probally saying I should not use what I am using but this is what I am being tought to use.
<?php
function dbConnect(){
// Connect to the database
$hostname="localhost";
$database="tblFile";
$mysql_login="*****";
$mysql_password="*****";
if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
echo"error on connect";
}
else{
if(!(mysql_select_db($database,$db))){
echo mysql_error();
echo "<br />error on database connection. Check your settings.";
}
else{
echo "I have successfully made a connection to my database and everything
is working as it should.";
}
}
$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");
$filename=filenameSafe($_FILES['upload']['name']);
$fileType=$_FILES["upload"]["type"];
if (in_array($_FILES["upload"]["type"],$aryImages)){
createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["upload"]["type"],$aryDocs)){
move_uploaded_file($_FILES['upload']['tmp_name'],
"/home/valerie2/public_html/elinkswap/snorris/upload/".$filename);
$aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}
else{
echo "File Uploaded";
}
function createThumb($type,$tmpname,$filename,$new_w,$new_h){
$thumbFilename="tmb-".$filename;
echo $type;
echo "<br>".$tmpname;
if (is_numeric(strpos($type,"jpeg"))){
$src_img=imagecreatefromjpeg($tmpname);
}
if (is_numeric(strpos($type,"png"))){
$src_img=imagecreatefrompng($tmpname);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (is_numeric(strpos($type,"jpeg"))){
imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$thumbFilename);
imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$filename);
}
if (is_numeric(strpos($type,"png"))){
imagepng($dst_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$thumbFilename);
imagepng($src_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
dbInsert($filename,$thumbFilename,$type);
}
function filenameSafe($filename)
{
// Lower case
$filename = strtolower($filename);
// get extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
// Replace spaces with a ’_’
$filename = str_replace(" ", "_", $filename);
// Replace non-alphanumerics (except underscores)
$filename = preg_replace('/\W/', '', $filename);
// append the timestamp
$filename = $filename . time();
// create an md5 hash
$result = md5($filename);
// ensure the string is safe for the db query
$result = mysql_real_escape_string($result);
dbConnect();
$SQL="SELECT fileId FROM tblFile WHERE fileName='".$result.".$ext'";
$rs = mysql_query($SQL);
if (mysql_num_rows($rs) > 0) {
$result = str_replace(".$ext", time(), $result);
$result = "$result.$ext";
}
return $result;
}
function dbInsert($filename,$thumbFilename,$type){
dbConnect();
$SQL="INSERT Into tblFile (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
//echo $SQL;
mysql_query($SQL);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
Select File: <input type="file" name="upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="Upload">
</form>
</html>
I have look up myself to see if there is anyway of debugging in textpad but I am getting nothing.

IMHO the easiest way to debug PHP with a non-PHP-IDE is establishing an external logfile.
In your .htaccess you can specify something like
php_value display_errors 1
php_value error_reporting 2147483647
php_value error_log /var/log/php/php_error.log
Make sure this file is writable for your webserver/php process.
Withing your code you can simply use the method error_log
to log stuff into your file.

I'm afraid you have to switch to a "bigger" IDE to be able to do some serius debugging. You must try Eclipse or NetBeans with Xdebug. If you do a Google search you will find lot of tutorial to setup a debugging environment with eclipse php xdebug or netbeans php xdebug.
If you need something very very simpler, but better than calling everytime an echo or a var_dump, you should try FirePHP with FirePHP extension for Firefox.

just by a simple copy paste in a editor with syntax highlight I saw that all the functions you wrote are in the dbConnect function and this doesn't look normal.
also, if you would indent your code it would be a lot easier to read it, so to spot problems. if your server has error reporting and error display on you should get some messages

Related

PHP Post data not received

i'm currently working on a small script for my Homepage but i ran into a problem.
I Try to upload an Image, but it seems like the POST data from the form is not being received. What did i do wrong?
I already changed the post_max_size and everything in the php.ini.
These are the Errors i get:
"Notice: Undefined index: image in ...." & "Notice: Undefined index:
submit in ...."
<form method="POST" action="/eye/sites/handling/post.php" enctype="multipart/form-data">
<div class="fileUpload">
<span><i class="fa fa-folder-o" aria-hidden="true"></i> Bild wählen</span>
<input type="file" class="upload" name="image"/>
</div>
<input type="submit" value="Upload It!" name="submit"/>
</form>
<?php session_start();
error_reporting(E_ALL);
if (isset($_SESSION["login_stat"])) {
date_default_timezone_set('Europe/Berlin');
$config = "$_SERVER[DOCUMENT_ROOT]/eye/more/config.xml";
$xml = simplexml_load_file($config);
$picWidth = $xml->pic->width;
$picHeight = $xml->pic->height;
$fulldate = date('dmYHis');
if(isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$typeCheck = $_FILES['image']['type'];
if ($typeCheck != "image/jpeg") {
$error = "Not a .jpg";
header('location: /eye/sites/post.php?stat=bad&error='.$error);
exit;
}
$file = $_SERVER['DOCUMENT_ROOT']."/uploads/".$fulldate.".jpg";
$type = "image/jpeg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
$file_thmb = $_SERVER['DOCUMENT_ROOT']."/uploads/!1A_thmb/".$fulldate.".jpg";
include "resize-class.php";
$resizeObj = new resize($file);
$resizeObj->resizeImage($picWidth, $picHeight, 'crop');
$resizeObj->saveImage($file_thmb, 100);
// header('location: /eye/sites/post.php?stat=good');
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No File');
}
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No Data');
echo $_SERVER['CONTENT_TYPE'];
echo "<br>";
echo $_FILES['image']['tmp_name'];
echo "<br>";
echo $_POST['submit'];
echo "<br>";
}
} else {
header('location: /eye/index.php?stat=in');
}
?>
Edit:
The problem is definitely about my Localhost.
This whole thing is working fine on my Webspace, but on my localhost it's not working.
BUT: I'm not getting errors anymore, when is click on Submit it goes to the php file that should save the image, but nothing is happening. I just see a white Page.
But like i said, it runs perfectly on my webspace..
If this is running on your local machine, do a quick check to make sure your "php.ini" file is configured to allow file uploads.
php.ini
file_uploads = On
The codes look fine. Check if your form action is posting to the correct path and if I may suggest using a simpler approach to test your file upload function before making it more complex. Use the following to start testing.
if (isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
echo "Upload is working!";
}
}
Keep us updated on your findings.
Perhaps this general information will help someone, as it helped me: a submitted form will only include fields that have defined 'name' attributes. 'id' is not enough.
The idea is that 'id' identifies an element in the DOM for use by JavaScript (either as a global variable or for use in document.getElementById(ID)), but 'name' identifies those elements whose names and values will be sent to the destination ('action') page.
So it makes sense that there are two different identifying attributes, used in two different ways.

file upload return to upload html page

I'm looking to return to the previous page after a file upload and have "file uploaded successfully" on the upload page.
In upload.php at the top I have placed
sesssion_start();
And at the end of the file upload script I have placed
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
Now I know i need to put some code into the html document but unsure what needs to go in. Below is my html form script
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
I know it is going to be something similar to this but unsure how or where I would place it.
session_start();
if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
echo "File uploaded successfully";
}
If someone could walk me through adding the HTML code into the correct place I will be very greatful
After the comments i amend my php code to look like this.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
sesssion_start();
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'] ). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
exit();
And the syntax inside the stream.php to:
<?phpsession_start();
if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
echo "File uploaded successfully";
}
?>
Thanks,
Mark
Nota: You also cannot use echo and header together because that would considered as outputting before header, so we'll just use a session array as the message and the header to redirect to "upload_form.php", then show the respective message on that page afterwards.
Use session_destroy() also to destroy any previous sessions.
Sidenote: Use two seperate files.
HTML form: call this "upload_form.php"
<?php
session_start();
session_destroy();
?>
<form action="stream.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File">
</form>
<?php
if(isset($_SESSION['upload_success'])){
echo $_SESSION['upload_success'];
}
else{
echo "Please select a file.";
}
?>
PHP (file 2): call this "stream.php"
<?php
session_start();
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{
$_SESSION['upload_success'] = "File successfully uploaded.";
header("Location: upload_form.php");
exit;
}
else {
$_SESSION['upload_success'] = "Sorry, there was a problem uploading your file.";
header("Location: upload_form.php");
exit;
}
Edit:
Modify and add the following after if(move_uploaded_file...
if(isset($_FILES['uploadedfile']) && !empty($_FILES['uploadedfile'])){
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name']);
}
Your code works fine, but you should remove session['upload_success'] with unset function after you do echo success message.
try
unset( $_SESSION['upload_success'])
in stream.php right after
echo "File uploaded successfully";
update :
if you want to work all these on a single page, You can simply do it like below:
if(isset($_SESSION['upload_success']) and $_SESSION['upload_session'])
{
//echo success message
//remove session
}
if(isset($_POST['file'])){
//upload process , if it was successfull make seesion true...
}
else {
//show form
}
For a quick solution, you could use Ravi Kusuma's jQuery File Upload Plugin or an AJAX solution to do this.
Another alternative, though, to those proposed above is to programmatically construct / output an HTML form with some javascript, and get it to POST a message to stream.php:
CAVEAT: I haven't tried this myself, but I can't think why it wouldn't work. Would someone please confirm my sanity? -- Tested it myself: it works.
<?php
//upload.php
//Do file upload stuff, then:
$out = '
<form id="frmUpOkay" action="stream.php" method="post">
<input name="upMsg" value="Upload Successful" />
</form>
<script type="text/javascript">
$(function(){
$("#frmUpOkay").submit();
});
</script>
';
echo $out;
?>
You must also add this bit to the top of the stream.php file:
<?php
if ( isset($_POST['upMsg']) && isset($_POST['upMsg']) != '' ){
$upMsg = $_POST['upMsg']; //you should sanitize this input
}else{
$upMsg = '';
}
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div>
Your normal website content is here.<br>
<br>
Upload message: <?php echo $upMsg; ?> <br>
<br>
</div>
</body>
Notes:
Above code uses jQuery, so you would need the jQuery library included on your upload.php page (as shown above).
Placing
$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");
At the end, I believe, would set true no matter what actually happened with the file's upload the reason being, there is not a condition being checked.
Unless the script has an exit command when it fails, it will eventually get to the part where it says: "Set the upload success as true and then go to stream.php" rather than saying, "If the upload is successful, set the upload success as true and then go to stream.php"
I would try:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
if($_FILES['uploadedfile']['size'] == 0)//In other words, if no file was selected.
{
$_SESSION['upload_success'] = 4;//File wasn't selected
header("Location: stream.php");
exit();
}
if(!file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
{
$_SESSION['upload_success'] = (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],'upload/' . basename($_FILES['uploadedfile']['name'])) ? 1 : 2);
}
elseif(file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
{
$_SESSION['upload_success'] = 3;
}
header("Location: stream.php");
exit();
?>
Now in stream.php where you have your if statement that displays the message do this instead:
<?php
session_start();
switch (#$_SESSION['upload_success']) {
case 1:
echo "File uploaded successfully";
break;
case 2:
echo "Sorry, there was a problem uploading your file.";
break;
case 3:
echo "A file with that name already exists!";
break;
case 4:
echo "You must select a file to upload!";
break;
}
unset($_SESSION['upload_success']);
?>//So if you reload stream.php yet another time no messages will be displayed again for no reason. ie. none of the cases will match an unset variable.
Last, you cannot echo (or do any type of output meant to be viewed by a user) before you header(Location: "somepage.php");
The page will switch before the user can read the output.
The way your code is currently written in your question you could have the following happen:
The server echos "Sorry, there was a problem uploading your file", which will never be seen by the user.
$_SESSION['upload_success'] is then set to TRUE, which is obviously not in agreement with #1.
It then sends the user to stream.php where a success message is
displayed.
An alternate, lazier way with less useful scenario descriptions to also fix your problem would be to do this instead (in upload.php):
else
{
die("Sorry, there was a problem uploading your file.");
}
Hope that helps!

PHP:: File successfully uploaded (but not) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Wierd... I'm getting that the file was successfully uploaded but in the real world... nothing is uploaded...
Here is my code :
<?php
if
(
move_uploaded_file
(
$_FILES['myUploadedFile']['tmp_name'],
'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name'])
)
)
{
echo "The file ".$uploadedFile." has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?><!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="#" method="POST" enctype="multipart/form-data">
<input name="myUploadedFile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
Small tweak needed here :)
You need to split this up:
if (move_uploaded_file
(
$_FILES['myUploadedFile']['tmp_name'],
'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name'])
)) {
// ...
Instead, do:
$uploadedFile = basename($_FILES['myUploadedFile']['name']);
if (move_uploaded_file
(
$_FILES['myUploadedFile']['tmp_name'],
'gangina/' . $uploadedFile
)) {
// ...
When you do 'gangina/'.$uploadedFile=basename($_FILES['myUploadedFile']['name']) you're actually appending the declaration of $uploadedFile to gangina/, not the value of $uploadedFile. So this would in fact be evaluated to something like gangina/1 because the declaration of $uploadedFile succeeds, gives true, which is evaluated to 1.
You also need to check that the webserver (normally user www-data on linux) has the rights to create new files in the gangina folder (and that that folder actually exists). Under Windows this usually isn't an issue, unless you installed Xampp under Program Files.
Also have a look at this example code on php.net. That shows how to perform all necessary checks to give more precise feedback to the user when handling a file upload in PHP.
Give your submit button a name e.g. name="uploadImage"
<?php
function uploadImage($image,$ftp_file){
// Path and file name
$imgUrl = "gangina/".$image;
if (file_exists($imgUrl)){
$temp = str_ireplace('gangina/', '', $image);
$imgUrl = "gangina/". rand(1,99999).$temp;
}
$img = str_ireplace('gangina/', ' ', $imgUrl);
// Run the move_uploaded_file() function here
if(move_uploaded_file($ftp_file, $imgUrl)){
$results = "image successfully uploaded";
} else {
$results = 'Could not upload image';
}
return $results;
}
if(isset($_POST['uploadImage']{
$imgurl = $_FILES['ImageName']['name'];
$temp = $_FILES['ImageName']['tmp_name'];
//uploading image
uploadImage($imgurl, $temp);
}
?>

file upload issue in php

Ok I am to the point of pulling my hair out. I have been trying all day to get this to work right and nothing. Right now what this script is doing is when you go to upload the file it goes to the page and there is no image. I have no clue why aint it working right. I am still new at this file/image upload thing. I have tried a few different ways and nothing. Here is the upload.php code:
<?php
function dbConnect(){
// Connect to the database
$hostname="localhost";
$database="myDatabase";
$mysql_login="myLogin";
$mysql_password="myPassword";
if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
echo"error on connect";
}
else{
if(!(mysql_select_db($database,$db))){
echo mysql_error();
echo "<br />error on database connection. Check your settings.";
}
else{
echo "This is the home page. I have successfully made a connection to my database and everything
is working as it should.";
}
}
$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");
$filename=filenameSafe($_FILES['upload']['name']);
$fileType=$_FILES["upload"]["type"];
if (in_array($_FILES["upload"]["type"],$aryImages)){
createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["upload"]["type"],$aryDocs)){
move_uploaded_file($_FILES['upload']['tmp_name'],
"/home/valerie2/public_html/elinkswap/imagefolder/".$filename);
$aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}
else{
echo "File Uploaded";
}
}
function createThumb($type,$tmpname,$filename,$new_w,$new_h){
$thumbFilename="tmb-".$filename;
echo $type;
echo "<br>".$tmpname;
if (is_numeric(strpos($type,"jpeg"))){
$src_img=imagecreatefromjpeg($tmpname);
}
if (is_numeric(strpos($type,"png"))){
$src_img=imagecreatefrompng($tmpname);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (is_numeric(strpos($type,"jpeg"))){
imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
}
if (is_numeric(strpos($type,"png"))){
imagepng($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
imagepng($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
dbInsert($filename,$thumbFilename,$type);
}
function filenameSafe($filename) {
$temp = $filename;
// Lower case
$temp = strtolower($temp);
// Replace spaces with a ’_’
$temp = str_replace(" ", "_", $temp);
// Loop through string
$result = "";
for ($i=0; $i<strlen($temp); $i++) {
if (preg_match('([0-9]|[a-z]|_|.)', $temp[$i])) {
$result = $result.$temp[$i];
}
}
dbConnect();
$SQL="SELECT fileID FROM upload WHERE fileName='".$result."'";
//echo $SQL;
$rs=mysql_query($SQL);
echo mysql_num_rows($rs);
if(mysql_num_rows($rs)!=0){
$extension=strrchr($result,'.');
$result=str_replace($extension,time(),$result);
$result=$result.$extension;
}
return $result;
}
function dbInsert($filename,$thumbFilename,$type){
dbConnect();
$SQL="INSERT Into upload (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
//echo $SQL;
mysql_query($SQL);
}
?>
And this is my index.php code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
Select File: <input type="file" name="upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="Upload File">
</form>
</html>
Both these files are inside a folder name imagefolder only with a folder called upload. I have read so many things on the file/image and so many videos but I still aint understand why the picture wont show up.I even tried to do another way but it kept telling me invaild file type.
Your upload.php defines a lot of functions, but are they actually called somewhere? Some are calling each other, but none of them seems to be called from "always executed" code. You have to add some code which is executed in every case at executing this php file, e.g. after your dbInsert function:
function dbInsert($filename,$thumbFilename,$type){
dbConnect();
$SQL="INSERT Into upload (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
//echo $SQL;
mysql_query($SQL);
}
dbConnect();
But from a cursory glance I could't really determine if dbConnect is really the proper function to call - your functions seem a bit randomly interconnected; why is dbConnect calling createThumb, when createThumb is calling dbInsert, which in turn is calling dbConnect again? That will create an infinite recursion loop.
If I were you I'd start without any functions for testing the wanted behavior. You can always extract functionality to functions later on.
There seem to be a whole lot of functions, but they never get called, so nothing will happen I suppose.
Check the folder's Permission and simplify your code and try to echo something in each function in order to debug and then determine where does it stop then we can help you more
But most likely its a permission issue

Image upload form in PHP does nothing, no error

I have the following PHP code that I am using to upload an image to MySQL. When I click submit nothing happens.
include ("connect.php");
session_start();
$login = $_SESSION['wname'];
if((#$_POST['submit'])&&(isset($_FILES["myfile"]))){
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
if($name){
die("Error uploading file! Code $error.");
} else {
$place = "avatars/$name";
move_uploaded_file($tmp_name,$place);
$query = mysql_query("UPDATE page SET pid_image_name = '$place' WHERE wname = '$login' ");
die("upload complete <a href='index.php'>View image</a>");
echo "Upload complete!";
}
} else {
die("select a file");
}
Here's my form:
<form action='up.php' method='POST' enctype='multipart/form'>
<input type='file' name='myfile'>
<p> <input type='submit' name='submit' value="upload">
What am I doing wrong?
Another important parameter to check out in php.ini is post_max_size. If you set upload_max_filesize > post_max_size the result is that no data is written to $_POST nor $_FILES for files which sizes exceed post_max_size but are below upload_max_filesize
Format you're code.
Are you submitting the actual HTML form (not the PHP code) as html/multipart?? I am willing to be you're not. (enctype="multipart/form-data" needs added to your form tag!)
Learn to debug - actually put conditions elsewhere and see where your code is failing!
In your HTML for have you got enctype="multipart/form-data"?
Something like this:
<form action='submit.php' method='post' enctype="multipart/form-data">
Edit 1:
if you do this: if(move_uploaded_file($tmp_name, $place)){
echo "did move file<br />";
}else{
echo "move failed<br />";
}
You will get move failed (Or I do with your code)
Edit 2
I found your problem: you misspelt the temp-dir variable: you defined $temp but in the move_uploaded_file you asked for $tmp_name so here the correct code: move_uploaded_file($temp, $place);
Just in case you can't see any errors activate error reporting by setting error_reporting(E_ALL); right after the php tag.

Categories