...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
May 26, 2010

Simple PHP upload form not working

Question by Lea

I seem to run into so many issues when dealing with writing to directories. If someone could please look over this script, and tell me why it’s not working, I’d be so appreciative.

Upon uploading the file from the form, I don’t get anything.. It doesnt output any errors, it simply just refreshes.

Thanks, Lea

<?php include ('config.php');
 if( isset($_POST['submit']) ) {

$target = ''.$_SERVER['DOCUMENT_ROOT'].'/images/';
$target = $target . basename( $_FILES['photo']['name']) ;
$url = basename( $_FILES['photo']['name']) ;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
$moved =  "File has been moved to location $target";

$name = mysql_real_escape_string($_POST['photoname']);
mysql_query("INSERT INTO photos (photo_name, photo_image) VALUES ('$name',  '$url' )") or die(mysql_error());
$success = "Photo has been added!";

  } else {

$moved = "File has not been moved to $target";
$success = "Failed to upload:(";

  }

} ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<br /><br />
<b>Add a new photo</b>
<hr />
<br />
<b><?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?></b>
  <form enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF; ?>">
    <table cellspacing="0" cellpadding="0" width="500px">
      <tbody>
        <tr>
          <td valign="top">Photo Name:</td>
          <td valign="top">
            <input type="text" name="photoname" /><br />
            <br />
            </td>
        </tr>
        <tr>
          <td valign="top">Photo:</td>
          <td valign="top">
  <input type="file" name="photo"><br />
            <br />
            </td>
        </tr>
      </tbody>
    </table>
    <input type="submit" value="submit" />
  </form>
</div>
</body>
</html>

Answer by Victor Stanciu

Without checking the PHP code for errors, the first thing that stands out is that this:

isset($_POST['submit'])

always returns false. The input type=”submit” must have the name attribute “submit” in order to be sent:

<input type="submit" value="submit" name="submit" />

Answer by Starx

Ok done

<?php 
if( isset($_POST['submit']) ) {
 $target = 'images/';
 echo $target = $target . basename( $_FILES['photo']['name']) ;

 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
  $moved =  "File has been moved to location $target"; 
  $success = "Photo has been added!";
 } 
 else { 
 $moved = "File has not been moved to $target";
 $success = "Failed to upload:(";
  }
} 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
<meta name="robots" content="index, follow" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="<?php echo $globalurl; ?>styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="holder">
<b>Add a new photo</b>
<?php echo "$success<br />"; ?>
<?php echo "$moved<br />"; ?>
  <form enctype="multipart/form-data" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
    <table cellspacing="0" cellpadding="0" width="500px">
      <tbody>
        <tr>
          <td valign="top">Photo Name:</td>
          <td valign="top">
            <input type="text" name="photoname" /><br />
            <br />
            </td>
        </tr>
        <tr>
          <td valign="top">Photo:</td>
          <td valign="top">
    <input type="file" name="photo">
            </td>
        </tr>
      </tbody>
    </table>
    <input type="submit" value="submit" id="submit" name="submit" />
  </form>
</div>
</body>
</html>
Read more
May 25, 2010

CSS – Advantages of single image file vs multiple files

Question by Hamish

I have noticed that alot of sites now, have all of their images in single files are then use background-position to offset the rectangle which is shown on the screen.

Is this for performance reasons? If so why?

Answer by Oli

Is this for performance reasons? If so why?

  1. A single request means fewer concurrent connections to the server meaning other things can be loading. A TCP connection also costs resources on the server. Fewer total connections per user means more users can use it at once (if your performance is really connection bound).

  2. A single sprite usually has a slightly smaller filesize than a bunch of single images, though this is not guaranteed and different formats vary.

  3. Object states are all loaded at once meaning interaction is much more immediate than if you loaded another state on demand.

  4. Even if part of the sprite isn’t used on the current page, by loading it (and the browser caching it), this would speed up the browsing experience for the user later on when the user views other pages.

Sprites don’t fix everything and you probably shouldn’t get too anal about it until you’re forecasting heavy traffic and/or you’re moving to a CDN where you’re charged per request.

Of course if you have a whole load of 16×16 icons that get used in a single place, you could sprite them up very quickly. I find having several sprites for specific things like that actually makes the CSS a lot cleaner too.

Answer by Starx

And one more thing that @david didn’t mention is, its going to take a lot of server time to load several small images, instead load one big image in one go(request) and manipulate it as your choicee

Read more

How to know the file type of the file which you are forcing the user to download?

Question by Starx

I am trying to force the user to download a file. For that my script is:

$file = "filethis.zip";        
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip"); //This is what I need
header("Content-Transfer-Encoding: binary");
readfile($file);

The files I am going to upload my not be .zip all the time so I want to know the content type of the image I am going to receive in $file.
How to accomplish this

Answer by cjk

I always put application/octet-stream (or somwething like that) as the browser can’t then try and display it in any way, it will always force the user to Save / Run, then the file type is often inferred by the extension.

Answer by Starx

Ok, I did it myself.

I used

$type = filetype($file); //to know the type
Read more
May 24, 2010

Echo items with value of three

Question by user342391

I need to echo all items in my column that have the value of three. If a field has the value of three I then need to echo the ‘name’ and ‘description’ on that row.

This is what I have so far

$result = mysql_query($query1) or die(mysql_error());    
while ($row = mysql_fetch_array($result))    
{  
  echo $row['status'];
}

I need to write ‘if $row[`status´ == 3 echo ‘description’ ‘name’ else echo ‘no current staus’

I hope I made some sense because I am seriously confused

Answer by Starx

You can actually select the result which only has 3 value in its status

for that use this

$query = "SELECT * FROM yourtable WHERE status='3'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
      echo "Name: ".$row['name']."<br>Description: ".$row['description'];
}

and if you want to parse you result to display only those of value this then use this

$query = "SELECT * FROM yourtable";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array(result)) {
      if($row['status']==3) {
           echo "Name: ".$row['name']."<br>Description: ".$row['description'];
      }
}
Read more

use jquery to refresh div tab onclick?

Question by Q7898339

I’m using tabs, but wondering if it’s possible to “refresh” tab content onclick?

Answer by Starx

Yes it is possible, but you dont need onClick event for this I think

use this

<div id="example">
     <ul>
         <li><a href="ajaxpage1.php"><span>Content 1</span></a></li>
         <li><a href="ajaxpage2.php"><span>Content 2</span></a></li>
         <li><a href="ajaxpage3.php"><span>Content 3</span></a></li>
     </ul>
</div>

Notice that: instead of giving the id of the tab’s division, a page link given
so every time you click on the tabs, it updates it

this degrades gracefully – the links, e.g. the content, will still be accessible with JavaScript disabled.

ps: I am assuming that you having a
working tabs

Read more

css height and width problem

Question by Reigel

I have this HTML

<div class="someClass">
   <ul>
      <li><a href="index.html">1</a></li>
      <li><a href="index.html">2</a></li>
      <li><a href="index.html">3</a></li>
      <li><a href="index.html">4</a></li>
      <li><a href="index.html">5</a></li>
   </ul>
</div>

Problem:

I want to make the <a>s to shape like a square, with height = 20px and width = 20px.
I can make it have the height and width if I make it float: left. In there the problem occur cause I need the <ul> to be centered but it won’t because of the float:left of the <a>.

how can I make it centered with fix height and width of the anchors?

note: anchors don’t have fixed length… the sample is just 5, but it can also grow to 7, or 9. I also need the links to be inline in view.

Answer by Starx

Ok, done here is your answer

this is your markup

<div class="someClass">
   <ul>
      <li><a href="index.html">1</a></li>
      <li><a href="index.html">2</a></li>
      <li><a href="index.html">3</a></li>
      <li><a href="index.html">4</a></li>
      <li><a href="index.html">5</a></li>
   </ul>
</div>

Your CSS must be something like this

.someclass { 
    width:100%;
    overflow:hidden;
}
.someclass ul {
    position:relative;
    float:left;
    left:50%;
    list-style:none;
}

.someclass ul li {
    position:relative;
    float:left;
    right:50%;
}

.someclass ul li a {
     display:block;
     height:100px;
     width:100px;
     border: 1px #f00 solid;
}

This trick is completely flexible does not depend on how big your <UL> is

Read more

What is the need of JavaScript while developing a page?

Question

I have been developing websites for some time now and I hardly use any Javascript in my pages.

Whatever I can want to do with JavaScript, it is possible through PHP. Just like ajax itself. We can send a regular request instead of a ajax request, can’t we? We can use “include” to include sub part of pages.

So am i missing something about javascript, that I dont know of?

Answer by Starx

If you want your website to be more user friendly and easy to use you will need javascript.

Would you wait till this website (Stackoverflow.com) refresh your page, to see your comment updated?

Read more
May 22, 2010

PHP Link to session_destroy

Question by nosedive25

I need to make a link that when clicked will run this in php:

session_destroy();

I know how to make a link in html, but I don’t know how to make it interact with php. Thanks for any help.

Answer by Starx

For an example, you want to use this script for logging out.

Your HTML has to be something like this for “index.php” (just an example)

<a href="logout.php">Log Out</a>

Then on the “logout.php”

session_start(); //to ensure you are using same session
session_destroy(); //destroy the session
header("location:index.php"); //to redirect back to "index.php" after logging out
exit();

In case you want to use JavaScript, I can tell you that too?

Read more
May 21, 2010

Is there an easy way to do click-and-drag scrolling in text frames?

Question by Skilldrick

I have a div with overflow:auto and a scroll bar, and I’d like to be able to drag the contents to scroll. I don’t need to be able to select text. Is there an easy way to do this? A jQuery plugin would be good, otherwise plain old JavaScript would be fine.

It seems I haven’t made myself clear enough. There’s a div with a fixed height that I want to scroll. Instead of picking up the scroll bar, I want to click and drag inside the text in the opposite direction. Like on an iPhone. Like in Photoshop when you hold down space and drag.

-------------------
|               | |
|               | |
|               |||
|               | |
|         <----------- click here and drag to scroll.
|               | |
|               | |
-------------------

Answer by John Hartsock

Here is a nice implemenation of drag and scroll divs

http://plugins.jquery.com/project/Dragscrollable

Answer by Starx

There is plugin in Jquery UI called draggable
through this you can change any element into draggable object

Here is the link for the demo
http://jqueryui.com/demos/draggable/

It is as simple as this

<script type="text/javascript">
$(function() {
    $("#draggable").draggable();
});
</script>

It is also possible to make it scrollable inside a div. Check the demo page

Read more
...

Please fill the form - I will response as fast as I can!