hi, i am very very new to php.
have a some code like this

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if ($append_file_name == true && lentgh) {
			$save_name = $id . '_' . $file_name;
		}else {
			$save_name = $file_name;
		}

could you please tell me how or what can i do to add an if clause to say or anyother thing i can do, if the file name is longer then 40 chracters display to s user saying file name is too long please rename your file shorter and then upload again.

thank you

Recommended Answers

All 11 Replies

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if ($append_file_name == true) {
			$save_name = $id . '_' . $file_name;
		}else {
			$save_name = $file_name;
		}

sorry this is the correct code

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if(strlen($file_name) > 40)
			print("File name is too long please rename your file shorter and then upload again.\n");
		else if ($append_file_name == true) {
			$save_name = $id . '_' . $file_name;
		}else {
			$save_name = $file_name;
		}

i changed it to the new one you suggested but it syill uploaded the long name

any idea?

thank you

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if(strlen($file_name) > 40)
			die("File name is too long please rename your file shorter and then upload again.\n");
		else if ($append_file_name == true) {
			$save_name = $id . '_' . $file_name;
		}else {
			$save_name = $file_name;
		}

That would stop the rest of your code from executing, but it is a messy way to quit. Alternatively:

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if(strlen($file_name) > 40)
			print("File name is too long please rename your file shorter and then upload again.\n");
		else {
			else if ($append_file_name == true) {
				$save_name = $id . '_' . $file_name;
			}else {
				$save_name = $file_name;
			}
			// uploading code
		}
		// any finishing/cleaning up code you need to do

hi, thank you for trying to help but i don't think i am doing it right.
do you think it would be ok just to rename the users filename to date and time rather then asking them to change their file?
can i replace $real name with date and time? would you please tell me how?
thank you

$save_name = "$owner" . "_" . "$realname";

hi
the very first one, i made it work but it still uploads the file while saying it too long,

second one with die, worked the best but there is nothing but what w are printing

3rd gave me an error
Parse error: syntax error, unexpected T_ELSE in /data

any suggestion?

thank you

Sorry, I haven't tested these. The third one should be:

// Get file name from URL and remove any bad filename chars.
		$url_parts = explode('/', $url);
		$num = count($url_parts)-1;
		$file_name = $url_parts[$num];
		$badchararray = array(" ", "'", "\"", "$", "&", "%", "-", "#", "^", "*", "(", ")", "~", "@", "/", "?", "=");
		$file_name = str_replace($badchararray, "", $file_name);
		$file_name = stripslashes(strtolower($file_name));
		if(strlen($file_name) > 40)
			print("File name is too long please rename your file shorter and then upload again.\n");
		else {
			if ($append_file_name == true) {
				$save_name = $id . '_' . $file_name;
			}else {
				$save_name = $file_name;
			}
			// uploading code
		}
		// any finishing/cleaning up code you need to do

Just curious, what exactly happens with the second one?

i could not make it work, probaly i am doing something wrong

would you please tell me how would i save the file by dateandtime

i wanna take the file name and change it to date and time
thank you

What do you mean you can't make it work? Output/error messages are most helpful. And your statement would look like:

$save_name = date("d m y_s i H") . ".txt";

Check date for help formatting, pathinfo to properly get the file extension.

hi finally i got some result thanks to you i am using this

die("can i put a link here File name is too long please rename your file shorter and then upload again.\n");

/

i am ok for now but if i could put some kind of link under the " File name is too long please rename your file shorter and then upload again" it would be great so user could go back.

thank you

Why yes you can! (I suggest learning HTML more thoroughly)

die("File name is too long please rename your file shorter and then upload again: go <a href=\"previous.html\">back</a>\n");

Replace previous.html with the page you want to send the user to.

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.