Member Avatar for Puster

Hello!

Yes, well, I am started to make a upload fucntion.

question number one: I've made a if statement that should recognize what file extension the file has, if it has a .png extension it would be inserted into the picture database, and if it is a .txt file it will be inserted to another databas, but it is not worcking.
Can someone explain it to me?
my code:

$allowedExtensions = array("zip", "exe", "jpg","jpeg","JPG", "png", "gif", "txt", "odp");

function isAllowedExtension($fileName) {
  global $allowedExtensions;

  return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

	
	//Skjekker om bildet er tatt
	$check = mysql_query("SELECT * FROM upload_file WHERE name='$name'");
	if (mysql_num_rows($check)>=1)
		die ("Username is taken!");
		else {
if ($error > 0)
{
    die("Error Uploading Image! Code $error.");
}
else
if($allowedExtensions=='png')
{   move_uploaded_file($temp,"../uploaded/pictures/".$name);
		//setter inn dataen til databasen
$insert = mysql_query("INSERT INTO upload_file_png VALUES ('','$size','$name','20')") or die(mysql_error());
		echo "Fil er lagret";
		echo '<meta http-equiv="refresh" content="4; upload_file.php" />';
}
else {

2.
If i set a value to 20 in a database and every time someone is cliking on link like this : www.example.com/download?=name=123421.png and then the value will count down to 0

Thanks for answeres:)

Recommended Answers

All 2 Replies

Two points:

1. Keep your code simple unless you are really good (in which case you shouldn't be coming here asking for help).
2. The first skill is understanding the language syntax and writing the code. The second skill is learning how to debug so you don't have to come here asking basic questions. You seem to be getting there on the first part but you need to work on the second part.

Your code has some syntax errors but I presume that you knew that. Your statement on line 6 is more complex than it needs to be and you have an error buried within it. I changed it to make it simpler and I made one important change:

$wk = explode(".", $fileName);
     // end;
     if (in_array($wk[1],$allowedExtensions)) {
          return $wk[1];
     }
     else {
          echo "<br>Not Found ";
     }

and that works. The explode is putting two elements in the array (and you want the second one). Not sure why the end statement was there but it doesn't seem to be needed.

Simpler code allows you to see what is happening more clearly. If you don't see an obvious problem, then you need to insert some debugging statements (just an echo or something fancier if you are using some sort of debugging package) to see what results you are getting at key points in the program. If you do that, in most cases you will figure out your own error.

On line 11: $check = mysql_query("SELECT * FROM upload_file WHERE name='$name'"); Is $name being set to anything outside the script snippet you posted?

On line 20 $allowedExtensions=='png' will NEVER be true. $allowedExtensions is an array, not a string. Use a function like basename() or pathinfo() to get the filename extension. In you isAllowdExtension() function, don't forget to lowercase the extension (strtolower()) before testing it against the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.