We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5463
    • 33 Posts
    Hey - I’m having trouble deleting a image that was uploaded by one of the people editing the site - it’s a simple .jpg but it was called ’flags.JPG - I presume the appostrophe is is the problem - this causes the entire images folder to not display in firefox. I’ve tried deleting it through modx, dreamweaver and filezilla. I can’t seem to change the permissions on the actual file - keeps reverting to 644.

    Any ideas?

    Thanks,
    cliff.
      • 22303 MODX Staff
      • 10,725 Posts
      If you are on shared hosting and the file was uploaded by a PHP process, the file is owned by the web server user, which is likely ’nobody’ or ’apache’. In which case, you user account probably does not have access to delete that file. You will need to find a way to delete it with a php script, or ask your hosting provider to change ownership of the file so you can delete it with your user account.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You should have a control panel for your domain with a file manager that should be able to remove it.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 5463
          • 33 Posts
          Hey, thanks for the help on this grin

          Is there a way to stop people uploading files that contain bad characters like ’ or " or ! or ? etc? apart from telling them not to do it - cuz we tried that!!

          Thanks,
          Cliff.
            • 1511
            • 144 Posts
            If they’re uploading through your scripts (MODx), add a file name checking / replacing bad characters before saving the file on the server.

              • 5463
              • 33 Posts
              Hey andrazk, thanks for your help, however I’m pretty inexperienced with this type of thing - could you point me in the right direction? Also, instead of replacing the bad characters could I just have an alert come up saying something like "Cannot upload file with this filename...etc..."

              Thanks!!
                • 1511
                • 144 Posts
                Well, I’ve had 15 minutes of spare time and I’ve come up with the following HTML.
                This is a front-end checking, prior the file upload. If the client has JavaScript disabled it won’t work. My recommendation would be to double check it on the server.

                <!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-2" />
                <title>Test file name</title>
                </head>
                
                <body>
                
                <script language="javascript" type="text/javascript">
                
                // code from http://lawrence.ecorp.net/inet/samples/regexp-parse.php
                function xtractFile(data){
                	data = data.replace(/^\s|\s$/g, "");
                
                	if (/\.\w+$/.test(data)) {
                			var m = data.match(/([^\/\\]+)\.(\w+)$/);
                			if (m)
                				return {filename: m[1], ext: m[2]};
                			else
                				return {filename: "", ext:""};
                	} else {
                			var m = data.match(/([^\/\\]+)$/);
                			if (m)
                				return {filename: m[1], ext: ""};
                			else
                				return {filename: "", ext:""};
                	}
                }
                
                function checkFileNames() {
                
                var VALID_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
                
                  formInput = document.getElementById('ulFile');
                	fileName = formInput.value;
                	
                  pathinfo = xtractFile(fileName);
                  
                	testString = pathinfo.filename.toUpperCase()+pathinfo.ext.toUpperCase();
                	
                  badName = false;
                  for (c = 0; c < testString.length; c++) {
                	  if (VALID_CHARS.indexOf(testString[c]) < 0) {
                		  badName = true;
                			break;
                		}
                	}
                
                	if (badName) {
                	  alert('Invalid characters in file name! ('+pathinfo.filename+'.'+pathinfo.ext+')');
                	}
                	
                	return !badName;
                }
                </script>
                
                <form action="test.htm" onsubmit="return checkFileNames();" enctype="multipart/form-data" method="post">
                
                <input type="file" name="ulFile" id="ulFile" />
                <br /><br />
                <input type="submit" value="Upload file" />
                
                </form>
                
                </body>
                </html>


                It’s a web form for file uploading. Sort of. smiley

                Select a file, on submitting the form validate the filename and extension to the VALID_CHARS string and off you go. Stop the submit for an invalid filename.

                If you want to allow more characters in filename just add them to the constant VALID_CHARS. Use only UPPER CASE.
                If you want to upload more files, iterate through the filenames.
                If the filename string is empty nothing happens.

                Feel free to expand/modify the code, I hope it helps as an example.